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,
    email: Sequelize.STRING,
    percentage: Sequelize.FLOAT,
    text: Sequelize.TEXT,
    categories: Sequelize.TEXT,
    start: Sequelize.DATE,
    end: Sequelize.DATE
});

var Offer = sequelize.define('Offer', {
    name: Sequelize.STRING,
    deadline: Sequelize.DATE,
    optionDuration: Sequelize.INTEGER
});

Shop.hasMany(Offer);
Offer.belongsTo(Shop);

This creates the two tables shops and offers, both of them with only "id" primary key

I also have some n:m associations like:

Group.hasMany(Accesslevel);
Accesslevel.hasMany(Group);

but also in this case, in the join table that Sequelize creates, there are no foreign key; so if I delete for ex. an acccesslevel, than the corresponding records in the join table accesslevelsgroups are not deleted.

Does anybody know if I'm doing something wrong or missing something? What I need is to create all the foreign keys for the associations and the possibility to specify the behaviour 'onDelete' and 'onUpdate' (cascade)

-- UPDATE I've created a route for executing sync:

myServer.get('/sync', function (req, res) {
    sequelize.sync({force: true}).success(function() {
        console.log('sync done');
        res.send(200, 'sync done');
    }).error(function(error) {
        console.log('there was a problem');
        res.send(200, 'there was a problem');
    });
});

So then in the browser I type 127.0.0.1:port/sync to create the db structure


回答1:


Did you add the relation after creating the table ( in the database ) ?
You need .sync({ force: true }) if you modify your scheme's and run your program again. Sequelize will only create the table and all it's references if the table does not exist yet in the database.

Are you calling sync after all associations have been made ?

Are you using InnoDB ?

Oddly enough I can reproduce that, The easiest fix is to define the key manually I think, that's how I solved it, Might be a bug otherwise I'm not sure.

See here: http://sequelizejs.com/docs/latest/associations#block-3-line-24




回答2:


You should give foreign keys like that, it is not related to sync.

Offer.associate = function (models) {
    models. Offer.belongsTo(models. Offer, {
        onDelete: "CASCADE",
        foreignKey: 'shopId',
        targetKey: 'id'
    });
};



回答3:


This question was actually answered here

As trivial answers are converted to comments and it is hard to notice them under question I'll duplicate its main point here.

To add references constraints to the columns, you can pass the options onUpdate and onDelete to the association calls. . . .

User.hasMany(Task, { onDelete: 'SET NULL', onUpdate: 'CASCADE' })



来源:https://stackoverflow.com/questions/24309893/foreign-keys-with-sequelize-are-not-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!