module.exports = function(sequelize, DataTypes) {
var MenuTranslation = sequelize.define('MenuTranslation', {
name: DataTypes.STRING,
description: DataTypes.STRING
},{
tableName: 'menu_translations',
timestamps: false,
associate: function(models){
MenuTranslation.belongsTo(models.Menu, { onDelete: 'cascade' });
MenuTranslation.belongsTo(models.Language);
}
});
return MenuTranslation;
};
UPDATE: I add "onDelete: 'cascade'" into my MenuTranslation model. But this time it worked like onDelete: 'null'
onDelete option goes on the child object, not on the parent. It's as in SQL. Relevant documentation.
Related
This is my parent model Providers
module.exports = function(sequelize, DataTypes) {
const Providers = sequelize.define('Providers', {
provider_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
provider_name: DataTypes.STRING,
address: DataTypes.STRING,
city: DataTypes.STRING,
state: DataTypes.STRING,
zip: DataTypes.STRING,
phone: DataTypes.STRING,
website: DataTypes.STRING,
accepting_new_clients: DataTypes.INTEGER
});
Providers.associate = models => {
Providers.belongsTo(model.Providerclients, {foreignKey: "provider_id" })
}
return Providers;
}
This is my child model Providerclients
module.exports = function(sequelize, DataTypes) {
const Providerclients = sequelize.define('provider_clients', {
provider_client_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
provider_id: DataTypes.INTEGER,
client_id: DataTypes.INTEGER
},{});
Providerclients.associate = (models) => {
Providerclients.belongsToMany(models.Providers, {foreignKey: "provider_id"});
};
return Providerclients;
}
This is my other child model Providerinsurance
module.exports = function(sequelize, DataTypes) {
const Providerinsurance = sequelize.define('provider_insurance', {
provider_insurance_id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
provider_id: DataTypes.INTEGER,
insurance_id: DataTypes.INTEGER
},{});
return Providerinsurance;
}
I am trying to get data from providers table joining the provider clients table.
To be clear I want to run the below query in mysql format
select * from providers as p left join provider_clients as pc on p.provider_id = pc.provider_id left join provider_insurance as pi on p.provider_id = pi.provider_id
I tried to join in sequelize as you can see in Providers and Providerclients models. I got the below error
Unhandled rejection SequelizeEagerLoadingError: provider_clients is not associated to Providers!
These is an association issue in your structure. Change your association to hasMany.
provider.hasMany(clientProvider)
This will add provider id into client provider.
http://docs.sequelizejs.com/manual/associations.html#one-to-many-associations--hasmany-
Here is a summary
Player.belongsTo(Team) // `teamId` will be added on Player / Source model
User.belongsTo(Company, {foreignKey: 'fk_company'}); // Adds fk_company to User
User.belongsTo(Company, {foreignKey: 'fk_companyname', targetKey: 'name'}); // Adds fk_companyname to User
========================================================================
Coach.hasOne(Team) // `coachId` will be added on Team / Target model
============================================================================
Project.hasMany(User)
This will add the attribute projectId or project_id to User. Instances of Project will get the accessors getWorkers and setWorkers.
Project.hasOne(User)
hasOne will add an attribute projectId to the User model!
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?
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'});
i found many things about associations but nothing to my particular case,
i created some models and i'm trying to associate them,
so i think it is a matter of understanding database modeling too.
I've got the models user and staff, both share an attribute user_id.
user.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
user_id: DataTypes.STRING,
fullname: DataTypes.STRING,
username: DataTypes.STRING,
comment: DataTypes.TEXT
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
User.hasMany(models.Sshkey, {foreignKey: 'sshkey_id'})
}
}
});
return User;
};
staff.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var Staff = sequelize.define('Staff', {
user_id: DataTypes.STRING,
fullname: DataTypes.STRING,
username: DataTypes.STRING,
password: DataTypes.STRING,
isAdmin: DataTypes.BOOLEAN
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
Staff.hasMany(models.Sshkey, {foreignKey: 'sshkey_id'})
}
}
});
return Staff;
};
And i've got a model sshkey which can belong to either an user or a staff member.
I'm using Sequelize cli and haven't done any migrations yet.
And i'm pretty new to Js and creating databases, thinking about the database models and the associations, and i'm curios if i could write or do such thing as:
'use strict';
module.exports = function(sequelize, DataTypes) {
var Sshkey = sequelize.define('Sshkey', {
sshkey_id: DataTypes.STRING,
sshkey: DataTypes.TEXT
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
// My Problem starts here |
// Should i write |
// |
// V
Sshkey.hasOne(models.User || models.Staff, {foreignKey: 'user_id'})
// Or maybe:
// Sshkey.hasOne(models.User, {foreignKey: 'user_id'}) ||
// Sshkey.hasOne(models.Staff, {foreignKey: 'user_id'})
// Should i rather rename models.Staffs foreignKey user_id to staff_id?
// Or maybe:
// Sshkey.hasOne(models.User, {as: 'userkey', foreignKey: 'user_id'})
// Sshkey.hasOne(models.Staff, {as: 'staffkey', foreignKey: 'user_id'})
}
}
});
return Sshkey;
};
What would be a proper solution for the problem that if i later on want to reference a sshkey to either a user or a staff member?
Making two models with staffkeys and userkeys?
Thanks in advance,
BigZ
If you want a 1:m relationship where the foreign key user_id is added to the Sshkey model, should be:
User.hasMany(models.Sshkey, {foreignKey: 'user_id'});
Staff.hasMany(models.Sshkey, {foreignKey: 'user_id'});
Sshkey.belongsTo(models.User, {foreignKey: 'user_id'});
Sshkey.belongsTo(models.Staff, {foreignKey: 'user_id'});
One issue I have with your example is that user_id and sshkey_id are both strings with no constraints around them, which makes them very bad foreignKey's and terrible for designing databases. To get a User and their Sshkey:
User.findAll({
where: {},
include: [ { model: Sshkey } ]
});
I'm trying to create many to many relationship using Sequelize + nodeJs using existing MySQL Database :
Below is my tables :
- "usr" Table : usr_id (PK)
- Intermediate "usr_role" : usr_id, role_id
- "role" table : role_id
This is my models
"User" Models:
"use strict";
var bcrypt = require('bcryptjs');
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
usrid : {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
field:'usr_id'
},
name :{
type: DataTypes.STRING,
allowNull: false,
field:'usr_name'
},
}, {
timestamps: false,
freezeTableName: true,
tableName: 'usr',
name:'User',
underscored:'true',
classMethods: {
associate:function(models){
User.belongsToMany(models.Role, { through: 'UserRole', foreignKey:'usr_id', as:'UserRoles'});
}
}
}
);
return User;
};
"Role" models
module.exports = function(sequelize, DataTypes) {
var Role = sequelize.define('Role', {
id : {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false,
field:'role_id'
},
name :{
type: DataTypes.STRING,
allowNull: false,
field:'role_name'
},
},{
timestamps: false,
freezeTableName: true,
tableName: 'role_ref',
underscored:'true',
classMethods: {
associate:function(models){
Role.belongsToMany(models.User, {
through: 'UserRole',
foreignKey:'role_id'});
}
}
}
)
return Role;
};
E:\nodejsWS\learn\node_modules\inflection\lib\inflection.js:795
var str_path = str.split( '::' );
^
TypeError: Cannot read property 'split' of undefined
at Object.inflector.underscore
(E:\nodejsWS\learn\node_modules\inflection\lib\inflection.js:795:25)
at Object.module.exports.underscoredIf
(E:\nodejsWS\learn\node_modules\sequelize\lib\utils.js:28:27)
at new BelongsToMany (E:\nodejsWS\learn\node_modules\sequelize
\lib\associations\belongs-to-many.js:134:15)
at Mixin.belongsToMany
(E:\nodejsWS\learn\node_modules\sequelize\lib\associations
\mixin.js:264:21)
at sequelize.define.classMethods.associate
(E:\nodejsWS\learn\models\Role.js:29:12)
at E:\nodejsWS\learn\models\index.js:50:21
this is my index.js
// Import models
fs
.readdirSync(__dirname)
.filter(function(file) {
return (file.indexOf(".") !== 0) && (file !== "index.js");
})
.forEach(function(file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
//associate models
Object.keys(db).forEach(function(modelName) {
if ("associate" in db[modelName]) {
console.log(db);
db[modelName].associate(db);
}
});
Line 50 refer to db[modelName].associate(db), that cause the error.
Can somebody please guide me what's i'm doing wrong. Do i need to manually create intermediate Models 'UserRole'. Your help is very much appreciated. Thanks.
You need to specify the actual intermediate table's name in the belongsToMany config, or define the model UserRole. Also, unless your intermediate table has the fields, you'll probably also want to specify timestamps: false as well. So, for example:
Role.belongsToMany(models.User, {
through: 'usr_role',
foreignKey:'role_id',
timestamps: false
});
This will cause sequelize to use that table in the queries it builds without needing a model defined, and will prevent it from requesting the timestamp fields from either the User table or the intermediate.
I am using sequelize as my backend ORM.
But I have a problem when I want "where" with a join table. The associations are good but I didn't know how to do for the "where".
This my code :
router.get('/id_presta_struct_unit/:id_presta_struct_unit', (req, res) => {
models.structures.findAll({
include: {
required: false,
model: models.structures_proposer_prestations,
where: {
id_presta_struct_unit: req.params.id_presta_struct_unit
},
include: {
model : models.unites_facturation,
}
}
}).then(data => {
res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'});
res.end(JSON.stringify(data));
});
});
I got this request
SELECT * FROM structures AS structures LEFT OUTER JOIN structures_proposer_prestations AS structures_proposer_prestations ON structures.id_structure = structures_proposer_prestations.id_structure AND structures_proposer_prestations.id_presta_struct_unit = '1' LEFT OUTER JOIN unites_facturation AS structures_proposer_prestations.unites_facturation ON structures_proposer_prestations.id_unite = structures_proposer_prestations.unites_facturation.id_unite;
But i would like to get
SELECT * FROM structures AS structures LEFT OUTER JOIN structures_proposer_prestations AS structures_proposer_prestations ON structures.id_structure = structures_proposer_prestations.id_structure LEFT OUTER JOIN unites_facturation AS structures_proposer_prestations.unites_facturation ON structures_proposer_prestations.id_unite = structures_proposer_prestations.unites_facturation.id_unite WHERE structures_proposer_prestations.id_presta_struct_unit = '1';
I don't know what to do I didn't find a post with the same problem
Can anyone point me in the right direction?
Thank you in advance.
Edit:
The associations
models.structures_employer_ressources.hasMany(models.ressources, { foreignKey: 'id_ressource' });
models.ressources.belongsTo(models.structures_employer_ressources, { foreignKey: 'id_ressource' });
The model of ressources
module.exports = function(sequelize, DataTypes) {
return sequelize.define('ressources', {
id_ressource: {
type: DataTypes.STRING,
allowNull: false,
primaryKey: true
}
........
},{
tableName: 'ressources',
updatedAt: 'date_modification',
createdAt: 'date_creation'
});
};
And the model of structures_employer_ressources
module.exports = function(sequelize, DataTypes) {
return sequelize.define('structures_employer_ressources', {
id_structure: {
type: DataTypes.STRING,
allowNull: false,
primaryKey: true,
references: {
model :'structures',
key: 'id_structure'
}
},
id_ressource: {
type: DataTypes.STRING,
allowNull: false,
primaryKey: true,
references: {
model :'ressources',
key: 'id_ressource'
}
}
},{
tableName: 'structures_employer_ressources',
updatedAt: 'date_modification',
createdAt: 'date_creation'
});
};
If you feed an array into the where clause of the initial join, you can run a raw query against the joins.
Example:
models.structures.findAll({
where:[["[structures_proposer_prestations].[id_presta_struct_unit] = " + req.params.id_presta_struct_unit, null]],
include: {
required: false,
model: models.structures_proposer_prestations,
where: {
id_presta_struct_unit: req.params.id_presta_struct_unit
},
include: {
model : models.unites_facturation,
}
}
}
The array can also take in standard object syntax and will be combined by an AND. The null I am passing in is for paramaters, so It can definitely be optimised to take in the id as a paramater, just don't know the syntax off hand.