I am using Node, Express, Postgres and Sequelize
Hi I am using using Sequelize I am successful in creating the through table with the belongsToMany for my Postgres database. But I want to add an additional column to it but cant figure out how to do it and I have searched the web and found no answer.
I got these two tables:
- Customers
- Store Products
This here below is my Sequelize method of creating the third table using the belongsToMany
association (many-to-many) and I will be calling the table order_list.
customer.belongsToMany(storeProduct, {
through: 'order_list',
unique: false,
foreignKey: 'customers_cid',
// How can I add this additional column quantity to it?
quantity: { /// How do I add it?
type: DataTypes.INTEGER
}
});
storeProduct.belongsToMany(customer, {
through: 'order_list',
unique: false,
foreignKey: 'store_products_spid',
// How can I add this additional column quantity to it?
quantity: { /// How do I add it?
type: DataTypes.INTEGER
}
});
sequelize.sync({ alter: true });
So how can I successfully add the column I quantity with type of INTEGER to the this new order_list table?
Thanks in advance!
CodePudding user response:
You need to define OrderList
model explicitly with additional column and then use it in belongsToMany
in the option through
.
See Advanced Many-To_many guide in the official documentation.
Example:
const User_Profile = sequelize.define('User_Profile', {
selfGranted: DataTypes.BOOLEAN
}, { timestamps: false });
User.belongsToMany(Profile, { through: User_Profile });
Profile.belongsToMany(User, { through: User_Profile });