Last active
August 29, 2015 14:02
-
-
Save PaulMougel/239419e10347fe2aa61e to your computer and use it in GitHub Desktop.
Sequelize: TypeError: Cannot read property 'through' of null
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Sequelize = require('sequelize'); | |
var Promise = require('bluebird'); | |
var sequelize = new Sequelize('test', 'pmougel', '', { | |
logging: false, | |
dialect: 'postgres', | |
omitNull: true | |
}); | |
var User = sequelize.define( | |
'users', | |
{ | |
id: { | |
type: Sequelize.INTEGER, | |
autoIncrement: true, | |
primaryKey: true | |
}, | |
email: { | |
type: Sequelize.STRING, | |
allowNull: false | |
} | |
}, | |
{ timestamps: false } | |
); | |
var Image = sequelize.define( | |
'images', | |
{ | |
id: { | |
type: Sequelize.INTEGER, | |
autoIncrement: true, | |
primaryKey: true | |
}, | |
}, | |
{ timestamps: false } | |
); | |
var ReferenceData = sequelize.define( | |
'referencedata', | |
{ | |
id: { | |
type: Sequelize.INTEGER, | |
autoIncrement: true, | |
primaryKey: true | |
}, | |
type: { | |
type: Sequelize.STRING, | |
allowNull: false | |
} | |
}, { timestamps: false } | |
); | |
Image.hasMany(ReferenceData, {through: ReferenceData, onDelete: 'cascade'}); | |
User.hasMany(ReferenceData, {through: ReferenceData, onDelete: 'cascade'}); | |
ReferenceData.belongsTo(Image); | |
ReferenceData.belongsTo(User); | |
sequelize.sync({force: true}) | |
.then(function () { | |
return Promise.all([ | |
Image.create(), | |
User.create({email: '[email protected]'}) | |
]) | |
}) | |
.spread(function (image, user) { | |
image.getReferencedata() | |
}) | |
.catch(console.log.bind(console)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "sequelizetest", | |
"version": "0.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "BSD-2-Clause", | |
"dependencies": { | |
"sequelize": "~2.0.0-dev11", | |
"pg": "~3.2.0", | |
"bluebird": "~1.2.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment