这是我流行的示例数据库:
https://www.mysqltutorial.org/mysql-sample-database.aspx/
我需要在支付表中创建一个 customer_id 外键,使用 squelize :
payments.belongsTo(customers, {
foreignKey: 'customer_id',
});
不幸的是,foreignKey 列不是由 sequelize 自动创建的,但是如果我使用这个命令,它应该:sequelize.sync({ force: true })
该列已创建,但我的完整数据库内容已清除!非常感谢 !
P.S:表格中填充了示例数据。我不想清除这些数据。
P.S:我使用 https://github.com/sequelize/sequelize-auto 从现有数据库生成数据模型。
最佳答案
在您的模型付款中添加 foreignKey
和其他列,如下所示,您可以在您提供的存储库链接的 git readme 中看到此示例
示例 = https://github.com/sequelize/sequelize-auto#typescript
payments.belongsTo(customers, {
foreignKey: 'customer_id',
targetKey: 'id'
});
customer_id: {
type: DataTypes.INTEGER(),
allowNull: false,
references: {
model: 'customers',
key: 'id'
}
}
关于mysql - Sequelize 不会在填充表上创建外键列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64769885/