Sequelize.js foreign key

Before I had the same problem, and solved when I understood the functioning of settings Sequelize.

Straight to the point!

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

Maybe it helps you

Edit:

You can read this to understand better:

http://docs.sequelizejs.com/manual/tutorial/associations.html#foreign-keys

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)