且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Sequelize.js外键

更新时间:2023-02-02 23:33:30

在遇到相同问题之前,当我了解设置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