Sequelize : How to map a custom attribute in a pivot table - mysql

I've got this pivot table, which represents a many to many relationship with the models Person and Movie.
The thing is I want to get the role when I call the movies that get the persons associated. I tried this but it doesn't show the role :
models.Movie.findAll({
include: [{
model: models.Person,
as: 'persons',
through: {attributes: ["role"]}
}]
}).then(function(movies) {
res.json(movies);
});
Do I have to specify something in the models for the role ?

I finally managed to achieve this by creating a model for the pivot table movie_person with the role attribute as a string.
var MoviePerson = sequelize.define("MoviePerson", {
role: DataTypes.STRING
},
{
tableName: 'movie_person',
underscored: true
});
Then in my Movie model I added this
Movie.belongsToMany(models.Person, {
through: models.MoviePerson,
foreignKey: 'movie_id',
as: 'persons'
});
I had to do something obviously similar to this in my Person model and that's it !

For the purpose of those who will need this, there is a new method called 'magic methods'. I believe you have declared your many-to-many asociation
const movies = Movie.findAll();
const person = Person.findbyPk(personId);
const moviesPerson = movies.getPersons(person);

Related

Using a const instead of values from database in Sequelize Node.js

I'm working on a project that has some users and roles. I want to add association (belongsToMany) on users and roles. I'm using MySQL database and users are stored in users table. Currently i'm storing roles in database as well but i want roles to be stored in a file instead of database. Is there a way i could use a const instead of table from database.
index.js file:
...
db.user = require("./user.model.js")(sequelize, Sequelize);
db.role = require("./role.model.js")(sequelize, Sequelize);
db.role.belongsToMany(db.user, {
through: "user_roles",
foreignKey: "roleId",
otherKey: "userId"
});
db.user.belongsToMany(db.role, {
through: "user_roles",
foreignKey: "userId",
otherKey: "roleId"
});
role.model.js file
module.exports = (sequelize, Sequelize) => {
const Role = sequelize.define("role", {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
name: {
type: Sequelize.STRING
}
});
return Role;
};
One thing that you could do is eliminate the Role table, and just create a UserRole table with a belongsTo() association to the User table. (And associate User to it by using hasMany())
You could then make the "name" column in UserRole have the ENUM datatype, and have the model draw the values available in that ENUM from reading the file in the file system. (Alternately, you can hard-code the values into the ENUM and that would be the "file" where you're updating possible role names)
I'm not totally sure what problem this is looking to solve, though. This seems like it would lead to a decrease in performance overall, though it WOULD eliminate the need for a Many-to-Many relationship that can be frustrating to work with.

MySQL foreign key name, what is it?

I am using Sequelize, a nodejs ORM for mysql. Using mysql workbench I made an EEM diagram and pushed that design into the db, so far so good.
Now in Sequelize I have to tell it what the design of the DB looks like, part of that is telling it what foreign keys are called.
In Workbench there is a foreign key tab in the tablethere are variables formatted likefd_positions_tradingPLan1` but I never name that, in fact in my EEM diagram I have
Then if I go to that foreign keys tab at the bottom I get this. I am confused as to exactly what I should tell the ORM the foreign key is...
Let's take your positions Table as reference. To build your model on sequelize you have to do the following:
module.exports = (sequelize, DataTypes) => {
const Position = sequelize.define('Position', { // this is the name that you'll use on sequelize methods, not what you have on your db
// define your columns like this:
tradeName: { //the name of the variable that you'll use on sequelize and js
field: 'trade_name', //the actual name of your column on the table
type: DataTypes.STRING(128) // the data type
},
// .......
// for your foreignKeys you have to define the column like your other attributes.
userId: {
field: 'user_id',
type: DataTypes.INTEGER
},
}, {
tableName: 'positions', //this is the name of your table on the database
underscored: true, // to recognize the underscore names
createdAt: 'created_at', //
updatedAt: 'updated_at',
});
//now for your association let's say that you defined your USER table like this example.
Position.associate = (models) => {
// on the foreignKey value, youhave to put the same that you define above, and on the db.
Position.belongsTo(models.User, { as: 'User', foreignKey: 'user_id' });
//depending on your other relations, you are gonna use hasMany, hasOne, belongsToMany
};
return Position;
};
Sequelize does the association only one way, that means that on this example, you can't query with sequelize from User to Position, to be able to
have two way association you have to defined on both models.
User.associate = (models) => {
// on this case we use hasMany cause user can have many positions I suppose, if not, use hasOne
User.hasMany(models.Poisition, { as: 'positions', foreignKey: 'user_id' }); //remeber to use the same foreignKey name
};
UPDATE:
as is an identfier for Sequelize. Let's say you make two associations for the same model, later when you try to query one of this associations, you can specify the association that you want
User.associate = (models) => {
User.hasMany(models.Poisition, { as: 'positions', foreignKey: 'user_id' });
User.hasMany(models.Poisition, { as: 'customerPositions', foreignKey: 'customer_id' });
};
//the actual association call
User.findAll({
include:[{
model: db.user,
as: 'positions'
}, {
model: db.user,
as: 'customerPositions'
}]
})
Now for fk_positions_users1, this is an identifier for MySQL itself. Sequelize only check for the foreignKey and the models involve. Obviously when Sequelize create the reference, it gives a template name using the table and column name. I tried myself creating a new foreignKey on my table and then updating the model and everything goes fine. You should'nt have problems with that.

Sequelize : Two belongsToMany associate the same table

My user table need associate to two ManyToMany model.
that is user's collection and user's history,
the join table's column is the same (id, userId, albumId).
the two join table auto created by sequelize, but when I use User.addAlbum(), how can I add associate to specific table.
models.user.belongsToMany(models.album, { through: 'album_collections' })
models.user.belongsToMany(models.album, { through: 'album_play_record' })
now it will add to album_collections, how can I add new association to album_play_record
Try adding a new model named 'album_play_record' and associate it in belongsToMany
const AlbumPlayRecord = sequelize.define('album_play_record', {
title: Sequelize.STRING,
description: Sequelize.TEXT
});
models.user.belongsToMany(models.album, { through: models.album_play_record });
models.album_play_record.belongsTo(models.xxx);

sequelize multiple foreign key includes only one column

i have made two foreign keys from user table.
db.Subscription.belongsTo(db.User, {foreignKey: 'creatorId'});
db.Subscription.belongsTo(db.User, {foreignKey: 'subscriberId'});
during search query i get subscriberId column included instead of creatorId
Subscription.findAll({
where: {
subscriberId: req.decoded._id
},
include: [
{
model: User,
foreignKey: 'creatorId',
attributes: ['name', 'role', 'uid', 'imageUrl']
}
]
})
can someone please find out what i am doing wrong here.
Try setting a name for the associations so Sequelize has a better idea which association to include. You can do something like this to set the names on the associations...
db.Subscription.belongsTo(db.User, {
as: 'creator',
foreignKey: 'creatorId'
});
db.Subscription.belongsTo(db.User, {
as: 'subscriber',
foreignKey: 'subscriberId'
});
Then you can use those names in the query to get the specific association, as so...
Subscription.findAll({
include: {
model: User,
as: 'creator',
attributes: ['name', 'role', 'uid', 'imageUrl']
},
where: {
subscriberId: req.decoded._identer
}
});
When you have associations to the same table more than once setting a name helps the ORM determine which association to load. For the record, for all of the records that get returned you can access that association by accessing the .creator on the instance.
Good luck! :)

Sequelize belongsToMany not creating new table

Using sequelize, I expect this line:
m.User.belongsToMany(m.Company, {through: 'UserCompany'});
to generate a new table in my database called 'user_company' that would link the 'user' table and the 'company' table together. However, it isn't doing that. Am I misunderstanding the documentation when it says
This will create a new model called UserProject with with the
equivalent foreign keys ProjectId and UserId. Whether the attributes
are camelcase or not depends on the two models joined by the table (in
this case User and Project).
or am I doing something wrong?
Here are the relations I am setting up
m.Company.hasMany(m.User);
m.User.belongsToMany(m.Company, {
through: m.UserCompany
});
m.User.sync({force: true, match: /_test$/});
m.Company.sync({force: true, match: /_test$/});
m.UserCompany.sync({force: true, match: /_test$/});
Looks like I needed to create the UserCompany model manually. So, UserCompany.js looks like:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('UserCompany', {
}, {
freezeTableName: true,
paranoid: true
});
}
Then the belongsToMany automatically adds the correct columns to the table.