Cant make Associations sequelize - javascript

Im trying to create association between two tables using models that are generated by sequelize-cli. But it is not making the relations in db.
Model User
User.hasMany(models.Albums, {
foreignKey: 'user_id',
as: 'userAlbums'
});
};
Model Album
Albums.associate = function(models) {
// associations can be defined here
Albums.belongsTo(models.User, {
foreignKey: 'user_id',
onDelete: 'cascade'
});
};

Related

Sequelize: Using include in a model that represents an association table between two other models

My models are defined as follows:
/** operation_sav Has Many composant
*/
model.operation_sav.belongsToMany(model.composant, {
through: "operation_sav_composant_reserves",
});
/** composant Has Many operation_sav*/
model.composant.belongsToMany(model.operation_sav, {
through: "operation_sav_composant_reserves",
});
This is the definition of the association model operation_sav_composant_reserves:
module.exports = function (sequelize, DataTypes) {
var operation_sav_composant_reserves = sequelize.define(
"operation_sav_composant_reserves",
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false,
autoIncrement: true,
unique: true,
},
operationSavId: DataTypes.INTEGER, //refers to model 1
composantId: DataTypes.INTEGER, // refers to model 2
qte: DataTypes.INTEGER,
// delivres: DataTypes.BOOLEAN,
},
{ timestamps: true }
);
return operation_sav_composant_reserves;
};
What I am trying to do is get the data inside operation_sav_composant_reserves table, and include with it the data of table composant.
So this is what I tried:
models.operation_sav_composant_reserves
.findAll({
where: {
operationSavId: opSavId,
},
// EagerLoadingError [SequelizeEagerLoadingError]: composant is not associated to operation_sav_composant_reserves!
include: [models.composant],
})
I got that error as you see in the comment:
// EagerLoadingError [SequelizeEagerLoadingError]: composant is not
associated to operation_sav_composant_reserves!
Any idea how to solve this?
I have found the solution in the Sequelize documentation:
In order to use include like I have described above, the join association operation_sav_composant_reserves needs to be defined like this:
// Setup a One-to-Many relationship between User and Grant
User.hasMany(Grant);
Grant.belongsTo(User);
// Also setup a One-to-Many relationship between Profile and Grant
Profile.hasMany(Grant);
Grant.belongsTo(Profile);
Instead of:
User.belongsToMany(Profile, { through: Grant });
Profile.belongsToMany(User, { through: Grant });
Grant here is the join table.
From a database perspective, these produce the exact same result: A join table Grant with foreign keys that refer to the User and Profile table.
The only difference is:
// With the Many-to-Many approach, you can do:
User.findAll({ include: Profile });
Profile.findAll({ include: User });
// However, you can't do:
User.findAll({ include: Grant });
Profile.findAll({ include: Grant });
Grant.findAll({ include: User });
Grant.findAll({ include: Profile });
// On the other hand, with the double One-to-Many approach, you can do:
User.findAll({ include: Grant });
Profile.findAll({ include: Grant });
Grant.findAll({ include: User });
Grant.findAll({ include: Profile });
// However, you can't do:
User.findAll({ include: Profile });
Profile.findAll({ include: User });
// Although you can emulate those with nested includes, as follows:
User.findAll({
include: {
model: Grant,
include: Profile
}
}); // This emulates the `User.findAll({ include: Profile })`, however
// the resulting object structure is a bit different. The original
// structure has the form `user.profiles[].grant`, while the emulated
// structure has the form `user.grants[].profiles[]`.
The best of both worlds: the Super Many-to-Many relationship
We can simply combine both approaches shown above!
// The Super Many-to-Many relationship
User.belongsToMany(Profile, { through: Grant });
Profile.belongsToMany(User, { through: Grant });
User.hasMany(Grant);
Grant.belongsTo(User);
Profile.hasMany(Grant);
Grant.belongsTo(Profile);
This way, we can do all kinds of eager loading:
// All these work:
User.findAll({ include: Profile });
Profile.findAll({ include: User });
User.findAll({ include: Grant });
Profile.findAll({ include: Grant });
Grant.findAll({ include: User });
Grant.findAll({ include: Profile });

Trying to connect two database tables in Sequelize with a joiner table

I am trying to get a query of all merchants associated with a user.
There are three tables:
user_tbl, user_favorite_tbl, and merchant_tbl
For each, I have created a Sequelize instance.
User, UserFavorite, and Merchant.
I am trying to do a query that will get me, per user, every merchant favorited via the user_favorite_tbl.
Here is my code:
public async fetch_user_with_favorite() {
let favMerchants = await User.findAll({
attributes: ["given_name", "family_name", "email"],
include: [
{
model: UserFavorite,
attributes: ["merchant_id"],
include: [
{
model: Merchant,
attributes: ["name"]
}
]
}
]
});
return favMerchants;
}
And
User.belongsToMany(Merchant, { through: "user_favorite_tbl" });
Merchant.belongsToMany(User, { through: "user_favorite_tbl" });
However when I try to call the end point associated with this code, I get the following error:
SequelizeEagerLoadingError: user_favorite_tbl is not associated to user_tbl!
When I try to add this:
User.hasMany(UserFavorite, { foreignKey: "user_id" });
Merchant.hasMany(UserFavorite, { foreignKey: "merchant_id" });
I get this error:
SequelizeEagerLoadingError: merchant_tbl is not associated to user_favorite_tbl!
Where exactly am I going wrong?
You can define associations as below -
Many to many relationships can be achieved using two one to many relationships as below-
User.hasMany(UserFavorite, { foreignKey: 'user_id', targetKey: 'id'});
UserFavorite.hasMany(User,{ foreignKey: 'user_id',targetKey: 'id'});
Merchant.hasMany(UserFavorite, { foreignKey: 'merchant_id', targetKey: 'id'});
UserFavorite.hasMany(Merchant,{ foreignKey: 'merchant_id',targetKey: 'id'});
Your belongsToMany association looks correct, however, when you have belongsToMany, your findAll should not have the middle table in include. through is taking care of the middle table association internally.
let favMerchants = await User.findAll({
attributes: ["given_name", "family_name", "email"],
include: [
{
model: Merchant,
attributes: ["name"],
through: { attributes: [] } // if you do not need any metadata in user_favorite_tbl.
}
]
});
On a side note
User.belongsToMany(Merchant, { through: "user_favorite_tbl" });
This maybe better to use
User.belongsToMany(Merchant, { through: UserFavorite });

Sequelize.js: How to set foreignkey in two 1:N relationships between two models?

What I want to do is to make the relationships that User sell or buy Item. To do it with Sequelize and MySQL, I wrote code like this.
db.User.hasMany(db.Item);
db.Item.belongsTo(db.User, { as: "buyer" });
db.User.hasMany(db.Item);
db.Item.belongsTo(db.User, { as: "seller" });
However, this code makes an error. I think db.User.hasMany(db.Item) creates userId in Item, and the code make that happen twice. But the problem is, I don't want to add userId in Item, but buyerId and sellerId. How do I need to set options in db.User.hasMany(db.Item)?
Given that you have buyerId and sellerId columns in Item, this should do:
db.User.hasMany(db.Item);
db.Item.belongsTo(db.User, { as: "buyer", targetKey: "buyerId"});
db.Item.belongsTo(db.User, { as: "seller", targetKey: "sellerId" });
If not User.id is the primary key, you should also add foreignKey: [key name] to both options.
See reference manual
You can create two 1-M relationships between User and Item like this:
db.User.hasMany(db.Item, {
foreignKey: 'sellerId',
});
db.User.hasMany(db.Item, {
foreignKey: 'buyerId',
});
db.Item.belongsTo(db.User, {
foreignKey: 'sellerId',
constraints: false,
as: 'seller'
});
db.Item.belongsTo(db.User, {
foreignKey: 'buyerId',
constraints: false,
as: 'buyer'
});

Propagating cascading onDelete foreign key constraint using sequelize and MSSQL

I have a problem creating foreign key onDelete constraint using Sequelize on MSSQL db.
This is my use case: each company can have multiple users and those users can create multiple requests. Some requests can be created without a reference to the user, which is why I have both references to company and user in the request model. I want to achieve that once a company is deleted, all users from the company are deleted and all requests from the company are deleted.
These are my models defined in Sequelize:
Company model:
const CompanyModel = sequelize.define('company', {
... some props ...
});
CompanyModel.associate = function (models) {
CompanyModel.hasMany(models.user, {
as: 'users',
foreignKey: 'companyId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
CompanyModel.hasMany(models.request, {
as: 'requests',
foreignKey: 'companyId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
};
User model:
const UserModel = sequelize.define('user', {
... some props ...
});
UserModel.associate = function (models) {
UserModel.belongsTo(models.company, {
as: 'company',
foreignKey: 'companyId',
});
UserModel.hasMany(models.request, {
as: 'requests',
foreignKey: 'userId',
});
};
Request model:
const RequestModel = sequelize.define('request', {
... some props ...
});
RequestModel.associate = function (models) {
RequestModel.belongsTo(models.company, {
as: 'company',
foreignKey: 'companyId',
});
RequestModel.belongsTo(models.user, {
as: 'user',
foreignKey: 'userId',
});
};
However, when I try to sync my database, this is the error I'm getting:
RequestError: Could not create constraint or index. See previous errors
'IF OBJECT_ID(\'[request]\', \'U\') IS NULL CREATE TABLE [request] ([id] CHAR(36) , ... some props ..., [companyId] CHAR(36) NULL, [userId] CHAR(36) NULL, PRIMARY KEY ([id]), FOREIGN KEY ([companyId]) REFERENCES [company] ([id]) ON DELETE CASCADE, FOREIGN KEY ([userId]) REFERENCES [user] ([id]) ON DELETE SET NULL);'
If I comment the relationship to the user inside of the request model then syncing of the databases passes. The same thing happens if I comment onDelete foreign key constraint inside of the company model for the user. The foreign key constraint for request doesn't matter. I guess, the problem is that deleting company will delete user which might be the creator of the request. The logic is that those requests will be deleted as well, but the database cannot know if only users from the same company (which will be deleted) will be the owners of requests deleted as well for the company.
I've also tried setting allowNull to foreign key for the user in request model, but it didn't work.
Any suggestions on what should I do? I need to achieve that deleting a company deletes its users and requests.

allow null value for sequelize foreignkey?

How do I allow null for a foreignkey? I have a through and belongsToMany association:
Shelf
belongsToMany(Book, { through: Library, as: "Books"});
Book
belongsToMany(Shelf, { through: Library, as: "Shelves"});
Library
section: DataTypes.STRING,
// ... other fields
I would like to record a null on the bookId in Library:
Library.create({
section: "blah",
bookId: null,
shelfId: 3
});
Since Sequelize automatically creates the bookId and shelfId in the join table Library, how would I specify that I want to allow a null on the bookId foreignkey in Library?
I am no expert on node.js nor sequelize.js, but with one search in google I got the official documentation. You might need to pay some attention to this part of the code in the documentation:
Library.hasMany(Picture, {
foreignKey: {
name: 'uid',
allowNull: false
}
});
It seems you need to specify the {foreignKey: {name: 'bookId', allowNull: true} } in Library.
By default, the Sequelize association is considered optional. If you would like to make the foreign key required, you can set allowNull to be false.
Foo.hasOne(Bar, {
foreignKey: {
allowNull: false
}
});
See the docs

Categories

Resources