assotiating two tables in sequelize
up vote
-1
down vote
favorite
Im having problems associating 2 models. I will try to describe the problem as detailed as possible and hope you can help me.
I have 2 Models: Zone and PLZ (both are also tables in database-mysql).
There can be One Zone having many PLZs and one PLZ can belong to One zone.
On saving a zone with its PLZs I have a table called "zone_plz" with only two columns: zone_id and plz_id. Both have foreign keys to Zone.id and PLZ.id
Zone Model:
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const PLZ = require('../models/PLZ');
const zone_plz = require('../models/relations/zone_plzs');
const Zone = sequelize.define('zone',
name:
type: Sequelize.STRING,
allowNull: false
,
color:
type: Sequelize.STRING,
allowNull: false
,
timestamps: true
);
module.exports = Zone;
PLZ model:
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const Zone = require('../models/Zone');
const zone_plz = require('../models/relations/zone_plzs');
const PLZ = sequelize.define('plz',
plz:
type: Sequelize.INTEGER,
allowNull: false
,
city:
type: Sequelize.STRING,
allowNull: false
,
district:
type: Sequelize.STRING
);
module.exports = PLZ;
and this is the zone_plz model:
const Sequelize = require('sequelize');
const sequelize = require('../../util/database');
const PLZ = require('../../models/PLZ');
const Zone = require('../../models/Zone');
const zone_plz = sequelize.define('zone_plz',
zone_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: Zone,
key: 'id',
,
plz_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: PLZ,
key: 'id',
,
timestamps: true
);
module.exports = zone_plz;
and this is how I query it:
router.get('/', function (req, res, next)
zone_plz.findAll(
include: [
model: PLZ,
as: 'plz'
,
model: Zone,
as: 'zone'
]
).then((result) =>
res.send(status: true, data: result)
).catch(function (err)
next(err)
)
);
As you can see i want to return a zone with all the belonging plzs to the user.
Im new to sequelize and Im also not sure if this is the right approach. I get the error: plz is not associated to zone_plz!
Can anyone help me?
mysql node.js foreign-keys associations sequelize.js
add a comment |
up vote
-1
down vote
favorite
Im having problems associating 2 models. I will try to describe the problem as detailed as possible and hope you can help me.
I have 2 Models: Zone and PLZ (both are also tables in database-mysql).
There can be One Zone having many PLZs and one PLZ can belong to One zone.
On saving a zone with its PLZs I have a table called "zone_plz" with only two columns: zone_id and plz_id. Both have foreign keys to Zone.id and PLZ.id
Zone Model:
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const PLZ = require('../models/PLZ');
const zone_plz = require('../models/relations/zone_plzs');
const Zone = sequelize.define('zone',
name:
type: Sequelize.STRING,
allowNull: false
,
color:
type: Sequelize.STRING,
allowNull: false
,
timestamps: true
);
module.exports = Zone;
PLZ model:
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const Zone = require('../models/Zone');
const zone_plz = require('../models/relations/zone_plzs');
const PLZ = sequelize.define('plz',
plz:
type: Sequelize.INTEGER,
allowNull: false
,
city:
type: Sequelize.STRING,
allowNull: false
,
district:
type: Sequelize.STRING
);
module.exports = PLZ;
and this is the zone_plz model:
const Sequelize = require('sequelize');
const sequelize = require('../../util/database');
const PLZ = require('../../models/PLZ');
const Zone = require('../../models/Zone');
const zone_plz = sequelize.define('zone_plz',
zone_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: Zone,
key: 'id',
,
plz_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: PLZ,
key: 'id',
,
timestamps: true
);
module.exports = zone_plz;
and this is how I query it:
router.get('/', function (req, res, next)
zone_plz.findAll(
include: [
model: PLZ,
as: 'plz'
,
model: Zone,
as: 'zone'
]
).then((result) =>
res.send(status: true, data: result)
).catch(function (err)
next(err)
)
);
As you can see i want to return a zone with all the belonging plzs to the user.
Im new to sequelize and Im also not sure if this is the right approach. I get the error: plz is not associated to zone_plz!
Can anyone help me?
mysql node.js foreign-keys associations sequelize.js
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Im having problems associating 2 models. I will try to describe the problem as detailed as possible and hope you can help me.
I have 2 Models: Zone and PLZ (both are also tables in database-mysql).
There can be One Zone having many PLZs and one PLZ can belong to One zone.
On saving a zone with its PLZs I have a table called "zone_plz" with only two columns: zone_id and plz_id. Both have foreign keys to Zone.id and PLZ.id
Zone Model:
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const PLZ = require('../models/PLZ');
const zone_plz = require('../models/relations/zone_plzs');
const Zone = sequelize.define('zone',
name:
type: Sequelize.STRING,
allowNull: false
,
color:
type: Sequelize.STRING,
allowNull: false
,
timestamps: true
);
module.exports = Zone;
PLZ model:
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const Zone = require('../models/Zone');
const zone_plz = require('../models/relations/zone_plzs');
const PLZ = sequelize.define('plz',
plz:
type: Sequelize.INTEGER,
allowNull: false
,
city:
type: Sequelize.STRING,
allowNull: false
,
district:
type: Sequelize.STRING
);
module.exports = PLZ;
and this is the zone_plz model:
const Sequelize = require('sequelize');
const sequelize = require('../../util/database');
const PLZ = require('../../models/PLZ');
const Zone = require('../../models/Zone');
const zone_plz = sequelize.define('zone_plz',
zone_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: Zone,
key: 'id',
,
plz_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: PLZ,
key: 'id',
,
timestamps: true
);
module.exports = zone_plz;
and this is how I query it:
router.get('/', function (req, res, next)
zone_plz.findAll(
include: [
model: PLZ,
as: 'plz'
,
model: Zone,
as: 'zone'
]
).then((result) =>
res.send(status: true, data: result)
).catch(function (err)
next(err)
)
);
As you can see i want to return a zone with all the belonging plzs to the user.
Im new to sequelize and Im also not sure if this is the right approach. I get the error: plz is not associated to zone_plz!
Can anyone help me?
mysql node.js foreign-keys associations sequelize.js
Im having problems associating 2 models. I will try to describe the problem as detailed as possible and hope you can help me.
I have 2 Models: Zone and PLZ (both are also tables in database-mysql).
There can be One Zone having many PLZs and one PLZ can belong to One zone.
On saving a zone with its PLZs I have a table called "zone_plz" with only two columns: zone_id and plz_id. Both have foreign keys to Zone.id and PLZ.id
Zone Model:
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const PLZ = require('../models/PLZ');
const zone_plz = require('../models/relations/zone_plzs');
const Zone = sequelize.define('zone',
name:
type: Sequelize.STRING,
allowNull: false
,
color:
type: Sequelize.STRING,
allowNull: false
,
timestamps: true
);
module.exports = Zone;
PLZ model:
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const Zone = require('../models/Zone');
const zone_plz = require('../models/relations/zone_plzs');
const PLZ = sequelize.define('plz',
plz:
type: Sequelize.INTEGER,
allowNull: false
,
city:
type: Sequelize.STRING,
allowNull: false
,
district:
type: Sequelize.STRING
);
module.exports = PLZ;
and this is the zone_plz model:
const Sequelize = require('sequelize');
const sequelize = require('../../util/database');
const PLZ = require('../../models/PLZ');
const Zone = require('../../models/Zone');
const zone_plz = sequelize.define('zone_plz',
zone_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: Zone,
key: 'id',
,
plz_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: PLZ,
key: 'id',
,
timestamps: true
);
module.exports = zone_plz;
and this is how I query it:
router.get('/', function (req, res, next)
zone_plz.findAll(
include: [
model: PLZ,
as: 'plz'
,
model: Zone,
as: 'zone'
]
).then((result) =>
res.send(status: true, data: result)
).catch(function (err)
next(err)
)
);
As you can see i want to return a zone with all the belonging plzs to the user.
Im new to sequelize and Im also not sure if this is the right approach. I get the error: plz is not associated to zone_plz!
Can anyone help me?
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const PLZ = require('../models/PLZ');
const zone_plz = require('../models/relations/zone_plzs');
const Zone = sequelize.define('zone',
name:
type: Sequelize.STRING,
allowNull: false
,
color:
type: Sequelize.STRING,
allowNull: false
,
timestamps: true
);
module.exports = Zone;
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const PLZ = require('../models/PLZ');
const zone_plz = require('../models/relations/zone_plzs');
const Zone = sequelize.define('zone',
name:
type: Sequelize.STRING,
allowNull: false
,
color:
type: Sequelize.STRING,
allowNull: false
,
timestamps: true
);
module.exports = Zone;
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const Zone = require('../models/Zone');
const zone_plz = require('../models/relations/zone_plzs');
const PLZ = sequelize.define('plz',
plz:
type: Sequelize.INTEGER,
allowNull: false
,
city:
type: Sequelize.STRING,
allowNull: false
,
district:
type: Sequelize.STRING
);
module.exports = PLZ;
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const Zone = require('../models/Zone');
const zone_plz = require('../models/relations/zone_plzs');
const PLZ = sequelize.define('plz',
plz:
type: Sequelize.INTEGER,
allowNull: false
,
city:
type: Sequelize.STRING,
allowNull: false
,
district:
type: Sequelize.STRING
);
module.exports = PLZ;
const Sequelize = require('sequelize');
const sequelize = require('../../util/database');
const PLZ = require('../../models/PLZ');
const Zone = require('../../models/Zone');
const zone_plz = sequelize.define('zone_plz',
zone_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: Zone,
key: 'id',
,
plz_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: PLZ,
key: 'id',
,
timestamps: true
);
module.exports = zone_plz;
const Sequelize = require('sequelize');
const sequelize = require('../../util/database');
const PLZ = require('../../models/PLZ');
const Zone = require('../../models/Zone');
const zone_plz = sequelize.define('zone_plz',
zone_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: Zone,
key: 'id',
,
plz_id:
type: Sequelize.INTEGER,
allowNull: false,
references:
model: PLZ,
key: 'id',
,
timestamps: true
);
module.exports = zone_plz;
router.get('/', function (req, res, next)
zone_plz.findAll(
include: [
model: PLZ,
as: 'plz'
,
model: Zone,
as: 'zone'
]
).then((result) =>
res.send(status: true, data: result)
).catch(function (err)
next(err)
)
);
router.get('/', function (req, res, next)
zone_plz.findAll(
include: [
model: PLZ,
as: 'plz'
,
model: Zone,
as: 'zone'
]
).then((result) =>
res.send(status: true, data: result)
).catch(function (err)
next(err)
)
);
mysql node.js foreign-keys associations sequelize.js
mysql node.js foreign-keys associations sequelize.js
asked Nov 11 at 18:32
b0ss
255
255
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You created references, not associations. Please, first of all, read the differences: Sequelize model references vs associations
then create associations between plz & zone like:
plz.belongsToMany(zone, through: 'zone_plz');
zone.belongsToMany(plz, through: 'zone_plz');
Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
– b0ss
Nov 13 at 19:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You created references, not associations. Please, first of all, read the differences: Sequelize model references vs associations
then create associations between plz & zone like:
plz.belongsToMany(zone, through: 'zone_plz');
zone.belongsToMany(plz, through: 'zone_plz');
Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
– b0ss
Nov 13 at 19:18
add a comment |
up vote
0
down vote
You created references, not associations. Please, first of all, read the differences: Sequelize model references vs associations
then create associations between plz & zone like:
plz.belongsToMany(zone, through: 'zone_plz');
zone.belongsToMany(plz, through: 'zone_plz');
Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
– b0ss
Nov 13 at 19:18
add a comment |
up vote
0
down vote
up vote
0
down vote
You created references, not associations. Please, first of all, read the differences: Sequelize model references vs associations
then create associations between plz & zone like:
plz.belongsToMany(zone, through: 'zone_plz');
zone.belongsToMany(plz, through: 'zone_plz');
You created references, not associations. Please, first of all, read the differences: Sequelize model references vs associations
then create associations between plz & zone like:
plz.belongsToMany(zone, through: 'zone_plz');
zone.belongsToMany(plz, through: 'zone_plz');
answered Nov 13 at 4:53
Dmytro Mysak
325213
325213
Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
– b0ss
Nov 13 at 19:18
add a comment |
Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
– b0ss
Nov 13 at 19:18
Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
– b0ss
Nov 13 at 19:18
Im writing this in my zone_plz model but getting the error: PLZ.belongsToMany(Zone, through: 'zone_plz'); ^ TypeError: PLZ.belongsToMany is not a function at Object.<anonymous> (/Applications/MAMP/htdocs/transport/models/relations/zone_plzs.js:28:5)
– b0ss
Nov 13 at 19:18
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251874%2fassotiating-two-tables-in-sequelize%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown