不创建带有 Sequelize 的外键 [英] Foreign keys with Sequelize are not created

查看:56
本文介绍了不创建带有 Sequelize 的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器使用 Sequelize(使用 mysql 方言);在 Sequelize 的文档中写道:

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

这会创建两个表shops和offer,它们都只有id"主键

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

我也有一些 n:m 关联,例如:

I also have some n:m associations like:

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

但也是在这种情况下,Sequelize创建的连接表中,没有外键;所以如果我删除前.一个访问级别,比连接表accesslevelsgroups中对应的记录不被删除.

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.

有谁知道我是做错了什么还是遗漏了什么?我需要的是为关联创建所有外键以及指定行为onDelete"和onUpdate"(级联)的可能性

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

然后在浏览器中输入 127.0.0.1:port/sync 来创建数据库结构

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

推荐答案

创建表后(在数据库中)是否添加了关系?
如果您修改方案并再次运行程序,则需要 .sync({ force: true }).如果该表在数据库中尚不存在,Sequelize 只会创建该表及其所有引用.

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 ?

你在使用 InnoDB 吗?

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.

请看这里:http://sequelizejs.com/docs/latest/associations#block-3-line-24

这篇关于不创建带有 Sequelize 的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆