sequelize.js

Foreign keys with Sequelize are not created

五迷三道 提交于 2021-02-19 01:16:09
问题 I use Sequelize for my Server (with mysql dialect); in Sequelize's documentation is written that this: var Task = this.sequelize.define('Task', { title: Sequelize.STRING }) , User = this.sequelize.define('User', { username: Sequelize.STRING }) User.hasMany(Task) Task.belongsTo(User) creates automatically foreign key references with constraints; but for me this doesn't happen: var Shop = sequelize.define('Shop', { name: Sequelize.STRING, address: Sequelize.STRING, phone: Sequelize.STRING,

How to limit joined rows (many-to-many association) in Sequelize ORM?

北慕城南 提交于 2021-02-18 12:11:15
问题 There are two models defined by Sequelize: Post and Tag with many-to-many association. Post.belongsToMany(db.Tag, {through: 'post_tag', foreignKey: 'post_id', timestamps: false}); Tag.belongsToMany(db.Post, {through: 'post_tag', foreignKey: 'tag_id',timestamps: false}); On a "tag" page I want to get tag data, associated posts and show them with pagination. So I should limit posts. But If I try limit them inside "include" Tag.findOne({ where: {url: req.params.url}, include: [{ model : Post,

Node - associate tables with Sequelize

别来无恙 提交于 2021-02-15 07:28:28
问题 I'm trying to associate tables in such a way that: A group has many documents A document belongs to a group A group belongs to an user An user has many groups When i try to list all from table 'Group' including 'User', throws an error saying that User is not associated to Group. My models: Document.js const Document = conn.define('document', { title: { type: Sequelize.TEXT, allowNull: false }, content: { type: Sequelize.TEXT, allowNull: false }, footer: { type: Sequelize.TEXT, } }) Document

making a record unique by incrementing a number in javascript

南笙酒味 提交于 2021-02-11 18:09:43
问题 I wanted to increment a number to a filename if the filename already exists in the database (based on the records too). for example if I add file with filename DOC it will check if DOC exist since DOC exist on the example below and the latest increment is 1 (DOC-1) then the filename would be DOC-2. If I add DOC again and the latest increment is 2 so the new filename would be DOC-3.so on and so forth. Any idea guys ? thank you. Sounds like what I want is to always create a new filename (by

Sequelize query with NOT IN clause and nested SELECT

こ雲淡風輕ζ 提交于 2021-02-11 17:38:54
问题 I have an SQL query which I would like to express using the sequelize query syntax. DB Scheme Users: ( id , username, ...) Groups: ( id , name, ...) User_Groups: ( id , #groupId, #userId) Query Description Retrieve all users that are not yet part of a particular group. Raw SQL Query (working) SELECT User.id, User.email FROM Users as User WHERE User.id NOT IN ( SELECT User_Group.userId FROM User_Groups as User_Group WHERE User_Group.groupId = ${groupId} ) Where I'm blocked I tried to simulate

Sequelize hasMany is working fine but the inverse relation is not working

六月ゝ 毕业季﹏ 提交于 2021-02-11 15:54:00
问题 I am trying to work with mysql relations in Node Js(express Js) using Sequelize. User.hasMany(Post); work just fine, but when i try to inverse it in Post model like: Post.belongsTo(User); got this error: throw new Error(${source.name}.${_.lowerFirst(Type.name)} called with something that's not a subclass of Sequelize.Model); Error: post.belongsTo called with something that's not a subclass of Sequelize.Model User model like: const Sequelize = require('sequelize'); const db = require('..

Sequelize hasMany is working fine but the inverse relation is not working

血红的双手。 提交于 2021-02-11 15:52:48
问题 I am trying to work with mysql relations in Node Js(express Js) using Sequelize. User.hasMany(Post); work just fine, but when i try to inverse it in Post model like: Post.belongsTo(User); got this error: throw new Error(${source.name}.${_.lowerFirst(Type.name)} called with something that's not a subclass of Sequelize.Model); Error: post.belongsTo called with something that's not a subclass of Sequelize.Model User model like: const Sequelize = require('sequelize'); const db = require('..

POST http://localhost:3000/api/signup/ 404 (Not Found)

允我心安 提交于 2021-02-11 14:45:08
问题 i am trying to send data of the user from react to express and use the create function to create a new user but is says error 404 and page not found why? my api endpoint (found in client/src/components/api-users : async function create (data) { try { const resp = await fetch('/api/signup/' , { //error initiating here method:'POST', mode:"cors", credentials:'include', headers:{ 'Content-Type': 'application/json ', 'Accept': 'application/json', "Access-Control-Origin": "*" }, body:JSON

How to make Typescript transpile my Sequelize model?

假如想象 提交于 2021-02-11 14:25:19
问题 After transpiling the code using the command "yarn tsc", I tried to execute my code and got the error in the image below. When I went to check the reason for the error, I saw that my Sequelize model classes are not being stacked, they are empty. I couldn't find explanations anywhere about the reason for this, nor in the Sequelize documentation. Could anyone help? My dependencies "dependencies": { "@babel/plugin-proposal-class-properties": "^7.8.3", "@types/bcrypt": "^3.0.0", "@types/cors": "

docker-compose: nodejs container not communicating with postgres container

蹲街弑〆低调 提交于 2021-02-10 15:39:06
问题 I did find a few people with a slightly different setup but with the same issue. So I hope this doesn't feel like a duplicated question. My setup is pretty simple and straight-forward. I have a container for my node app and a container for my Postgres database. When I run docker-compose up and I see the log both containers are up and running. The problem is my node app is not connecting to the database. I can connect to the database using Postbird and it works as it should. If I create a