Skip to content

Instantly share code, notes, and snippets.

@mrotaru
Created October 30, 2013 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrotaru/7233452 to your computer and use it in GitHub Desktop.
Save mrotaru/7233452 to your computer and use it in GitHub Desktop.
cannot create a self-referencing has-many association with constrained foreign keys
var Sequelize = require('sequelize');
var sequelize = new Sequelize('db', 'user', 'password');
var Person = sequelize.define('Person', {
name: Sequelize.STRING,
});
var ChildrenPerson = sequelize.define('ChildrenPerson', {
PersonId: {
type: Sequelize.INTEGER,
references: "Person",
referencesKey: "id"
},
ChildrenId: {
type: Sequelize.INTEGER,
references: "Person",
referencesKey: "id"
}
});
Person.hasMany(Person, { foreignKeyConstraint: true, joinTableName: 'ChildrenPersons' });
sequelize.sync({force:true}).on('success', function(){
console.log('sync OK');
}).on('error', function(err){
console.log(err);
});
/*
results in error:
Executing: CREATE TABLE IF NOT EXISTS `ChildrenPersons` (`PersonId` INTEGER , `PersonsId` INTEGER , `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`PersonId`, `PersonsId`)) ENGINE=InnoDB;
Executing: DROP TABLE IF EXISTS `Persons`;
Executing: CREATE TABLE IF NOT EXISTS `Persons` (`name` VARCHAR(255), `id` INTEGER NOT NULL auto_increment , `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
TypeError: Cannot read property 'sync' of undefined
at exec (/srv/www.tc.com/node_modules/sequelize/lib/query-chainer.js:78:37)
at onSuccess (/srv/www.tc.com/node_modules/sequelize/lib/query-chainer.js:65:11)
at null.<anonymous> (/srv/www.tc.com/node_modules/sequelize/lib/query-chainer.js:86:15)
at EventEmitter.emit (events.js:95:17)
at null.<anonymous> (/srv/www.tc.com/node_modules/sequelize/lib/dao-factory.js:195:41)
at EventEmitter.emit (events.js:95:17)
at null.<anonymous> (/srv/www.tc.com/node_modules/sequelize/lib/query-interface.js:162:19)
at EventEmitter.emit (events.js:95:17)
at null.<anonymous> (/srv/www.tc.com/node_modules/sequelize/lib/query-interface.js:698:17)
at EventEmitter.emit (events.js:117:20)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment