MySQL Sequelize.js 外键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14169655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sequelize.js foreign key
提问by swampcypress
When using Sequelize.js, the following code doesn't add any foreign key on tables.
使用 Sequelize.js 时,以下代码不会在表上添加任何外键。
var MainDashboard = sequelize.define('main_dashboard', {
title: Sequelize.STRING
}, {
freezeTableName: true
})
MainClient.hasOne(MainDashboard, { foreignKey: 'idClient' })
MainDashboard.hasOne(MainClient, { foreignKey: 'clientId' })
sequelize.sync({ force: true })
Is there any way to force Sequelize.js to add these foreign key constraints?
有没有办法强制 Sequelize.js 添加这些外键约束?
回答by Ilsondotcom
Before I had the same problem, and solved when I understood the functioning of settings Sequelize.
在我遇到同样的问题之前,当我了解设置 Sequelize 的功能后就解决了。
Straight to the point!
开门见山!
Suppose we have two objects: Personand Father
假设我们有两个目标:人与父亲
var Person = sequelize.define('Person', {
name: Sequelize.STRING
});
var Father = sequelize.define('Father', {
age: Sequelize.STRING,
//The magic start here
personId: {
type: Sequelize.INTEGER,
references: 'persons', // <<< Note, its table's name, not object name
referencesKey: 'id' // <<< Note, its a column name
}
});
Person.hasMany(Father); // Set one to many relationship
Maybe it helps you
也许它可以帮助你
Edit:
编辑:
You can read this to understand better:
您可以阅读本文以更好地理解:
http://docs.sequelizejs.com/manual/tutorial/associations.html#foreign-keys
http://docs.sequelizejs.com/manual/tutorial/associations.html#foreign-keys
回答by codejockie
For Sequelize 4 this has been updated to the following:
对于 Sequelize 4,这已更新为以下内容:
const Person = sequelize.define('Person', {
name: Sequelize.STRING
});
const Father = sequelize.define('Father', {
age: Sequelize.STRING,
personId: {
type: Sequelize.INTEGER,
references: {
model: 'persons', // 'persons' refers to table name
key: 'id', // 'id' refers to column name in persons table
}
}
});
Person.hasMany(Father); // Set one to many relationship
Edit: You can read more on associations at https://sequelize.org/master/manual/assocs.html
编辑:您可以在https://sequelize.org/master/manual/assocs.html上阅读有关关联的更多信息
回答by Jan Aagaard Meier
I just tried to run your code, and the rows seem to be created fine:
我只是尝试运行您的代码,行似乎创建得很好:
CREATE TABLE IF NOT EXISTS `main_dashboard` (`title` VARCHAR(255), `id` INTEGER NOT NULL auto_increment , `idClient` INTEGER, PRIMARY KEY (`id`)) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `main_client` (`id` INTEGER NOT NULL auto_increment, `clientId` INTEGER, PRIMARY KEY (`id`)) ENGINE=InnoDB;
clientId
is added to main_client
, and idClient
is added to main_dashboard
clientId
添加到main_client
,并idClient
添加到main_dashboard
It seems you have slightly confused what the hasOne
method does. Each time you call hasOne
an association is created, so your code effectively associates the two tables twice. The method you are looking for is belongsTo
似乎您对该hasOne
方法的作用有些困惑。每次调用hasOne
都会创建一个关联,因此您的代码有效地将两个表关联了两次。您正在寻找的方法是belongsTo
If you want each client to have one dashboard, the code would be the following:
如果您希望每个客户端都有一个仪表板,则代码如下:
MainClient.hasOne(MainDashboard, { foreignKey: 'clientId' })
MainDashboard.belongsTo(MainClient, { foreignKey: 'clientId' })
This creates a clientId
field on the main_dashboard
table, which relates to the id
field of the main_client
table
这会clientId
在main_dashboard
表上创建一个字段,该id
字段与main_client
表的字段相关
In short belongsTo
adds the relation to the table that you are calling the method on, hasOne
adds it on the table that is given as argument.
简而言之,belongsTo
添加与您正在调用该方法的表的关系,hasOne
将其添加到作为参数给出的表中。
回答by Farm
You need to add foreignKeyConstraint: true
你需要添加 foreignKeyConstraint: true
Try:
尝试:
MainClient.hasOne(MainDashboard, { foreignKey: 'idClient', foreignKeyConstraint: true })
回答by Krishan Pal
It's amazingly simple.
这非常简单。
const MainDashboard = this.sequelize.define('main_dashboard', {/* attributes */})
, MainClient = this.sequelize.define('main_client', {/* attributes */});
MainDashboard.belongsTo(MainClient, { foreignKey: 'clientId' }); // Adds clientId to MainDashboard
It will link this as a foreign key and you may use it as an association. Let me know if I'm missing anything.
它将将此链接为外键,您可以将其用作关联。如果我遗漏了什么,请告诉我。