Sequelize js: create/update with foreign key

selfclose

Just want to CRUD and understand how it work.

model.wallet.belongsTo(model.users);

model.wallet.sync({force: true}).then(function () {
    return model.wallet.create({
        balance: 300,
        users: {id:1}
    }, {
        include: [ model.users ]
    })
});

I tried include: [ {model: model.users} ] But still got (NULL)

If you have solution and some example, It should be nice.

piotrbienias

According to the example in Sequelize documentation

Player.belongsTo(Team); // Will add a teamId attribute to Player to hold the primary key value for Team.

By default the foreign key for a belongsTo relation will be generated from the target model name and the target primary key name.

Assuming that those models are called player and team, and the primary key in model team is id. It means that if you use belongsTo on specified model (without any additional option), it will create an attribute with name <related_model_name><related_model_primary_key>, so if your user model is called users and it's primary key is id, then the foreign key created in wallet model would be called usersId.


If you want to create new wallet and assign it to user, you need to specify proper foreign key field in create attributes

model.wallet.create({
    balance: 300,
    usersId: 1
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Foreign Key in Sequelize

Sequelize, foreign keys as composite primary key

Sequelize foreign key error on Heroku but not local testing

sequelize - Cannot add foreign key constraint

sequelize foreign key target vs source

graphql with sequelize join foreign key

Cannot add foreign key constraint Sequelize

Sequelize include related model on extended foreign key

Foreign Key with Sequelize not working as expected

Sequelize - How to setup foreign key and join on it

Error when trying to insert row to table because of UUID foreign key with sequelize.js

Return table data of foreign key with Sequelize

Sequelize relations foreign key constraint

How to create a foreign key on the same table with Sequelize?

Sequelize foreign key reference composite primary key

Sequelize and Postgres - foreign key constraint cannot be implemented

Sequelize associations not generating foreign key

Is it possible to force a foreign key to exist using Sequelize?

duplicate foreign key column using sequelize

Foreign Key definitions on Sequelize Models

Sequelize belongsToMany self reference not creating foreign key

Creating Primary and Foreign Key relations in Sequelize

Sequelize association with one foreign key

Problems with query using foreign key sequelize

Sequelize - foreign key on create include returns null

MySQL query in Sequelize ORM with foreign key

Sequelize referencing to wrong foreign key

identify how to retrieve result set data (node.js + sequelize + foreign key mysql)

Change Sequelize default foreign key with through tables