Sequelize.js外键 [英] Sequelize.js foreign key
本文介绍了Sequelize.js外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Sequelize.js时,以下代码未在表上添加任何外键.
When using Sequelize.js, the following code doesn't add any foreign key on tables.
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 })
有什么方法可以强制Sequelize.js添加这些外键约束吗?
Is there any way to force Sequelize.js to add these foreign key constraints?
推荐答案
在遇到相同问题之前,当我了解设置Sequelize的功能时就解决了.
Before I had the same problem, and solved when I understood the functioning of settings Sequelize.
直截了当!
假设我们有两个对象:人和父亲
Suppose we have two objects: Person and 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
也许对您有帮助
您可以阅读以下内容以更好地理解:
You can read this to understand better:
http://docs.sequelizejs.com/manual/tutorial /associations.html#foreign-keys
这篇关于Sequelize.js外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文