我正在使用 Node、Express、Postgres 和 Sequelize
嗨,我正在使用 Sequelize 我成功地為我的 Postgres 資料庫創建了帶有belongsToMany 的直通表。但我想向其中添加一個額外的列,但無法弄清楚如何去做,我在網上搜索并沒有找到答案。
我得到了這兩個表:
- 顧客
- 商店產品
下面是我使用belongsToMany關聯(多對多)創建第三個表的 Sequelize 方法,我將呼叫表 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 });
那么我怎樣才能成功地將型別為 INTEGER 的 Iquantity 列添加到這個新的 order_list 表中呢?
提前致謝!
uj5u.com熱心網友回復:
您需要OrderList使用附加列顯式定義模型,然后belongsToMany在選項中使用它through。
請參閱官方檔案中的高級多對多指南。
例子:
const User_Profile = sequelize.define('User_Profile', {
selfGranted: DataTypes.BOOLEAN
}, { timestamps: false });
User.belongsToMany(Profile, { through: User_Profile });
Profile.belongsToMany(User, { through: User_Profile });
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/318868.html
標籤:节点.js PostgreSQL sequelize.js 多对多 协会
下一篇:<p><strong>問題:</strong> 當使用<code>ggplotly</code>與<code>geom_tile
