最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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
});
发布评论

评论列表(0)

  1. 暂无评论