javascript - Sequelize js: createupdate with foreign key - Stack Overflow

Just want to CRUD and understand how it work.model.wallet.belongsTo(model.users);model.wallet.sync({for

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.

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.

Share Improve this question asked Feb 16, 2017 at 5:02 selfcloseselfclose 1213 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

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
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745159961a4614344.html

相关推荐

  • javascript - Sequelize js: createupdate with foreign key - Stack Overflow

    Just want to CRUD and understand how it work.model.wallet.belongsTo(model.users);model.wallet.sync({for

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信