Last active
December 24, 2015 09:39
-
-
Save FredrikAppelros/6779186 to your computer and use it in GitHub Desktop.
sequelize association problem
This file contains 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
Sequelize = require 'sequelize' | |
sequelize = new Sequelize 'sequelize-test', 'postgres', 'changeme123', | |
host: 'localhost' | |
port: 5432 | |
dialect: 'postgres' | |
A = sequelize.define 'a', name: Sequelize.STRING | |
B = sequelize.define 'b', name: Sequelize.STRING | |
A.belongsTo B, as: 'relation1' | |
A.hasMany B, as: 'relation2' | |
B.hasMany A, as: 'relation2' | |
sequelize.sync().success -> | |
chainer = new Sequelize.Utils.QueryChainer [ | |
A.create name: 'a1' | |
A.create name: 'a2' | |
B.create name: 'b1' | |
B.create name: 'b2' | |
] | |
chainer.run().success (res, a1, a2, b1, b2) -> | |
chainer = new Sequelize.Utils.QueryChainer [ | |
a1.setRelation1 b1 | |
a2.setRelation1 b2 | |
b1.setRelation2 [a1] | |
b2.setRelation2 [a1, a2] | |
] | |
chainer.run().success -> | |
console.log 'created database' |
This file contains 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
Sequelize = require 'sequelize' | |
sequelize = new Sequelize 'sequelize-test', 'postgres', 'changeme123', | |
host: 'localhost' | |
port: 5432 | |
dialect: 'postgres' | |
A = sequelize.define 'a', name: Sequelize.STRING | |
B = sequelize.define 'b', name: Sequelize.STRING | |
A.belongsTo B, as: 'relation1' | |
A.hasMany B, as: 'relation2' | |
B.hasMany A, as: 'relation2' | |
sequelize.sync().success -> | |
chainer = new Sequelize.Utils.QueryChainer [ | |
A.find where: {name: 'a1'} | |
A.find where: {name: 'a2'} | |
B.find where: {name: 'b1'} | |
B.find where: {name: 'b2'} | |
] | |
chainer.run().success (res, a1, a2, b1, b2) -> | |
console.log a1.name | |
console.log a2.name | |
console.log b1.name | |
console.log b2.name | |
chainer = new Sequelize.Utils.QueryChainer [ | |
a1.getRelation1() | |
a2.getRelation1() | |
b1.getRelation2() | |
b2.getRelation2() | |
] | |
chainer.run().success (res, i1, i2, is1, is2) -> | |
console.log i1 | |
console.log i2 | |
console.log is1.map (i) -> i.name | |
console.log is2.map (i) -> i.name |
This file contains 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": "sequelize-association-problem", | |
"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-beta.0", | |
"pg": "~2.6.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment