这是我当前的迁移
向上:异步(queryInterface,Sequelize)=> {
return Promise.all([
queryInterface.addColumn(
'carts',
'user_id',
{
type: Sequelize.INTEGER,
allowNull: false
}
).then(() => {
queryInterface.addConstraint('carts', {
fields: ['user_id'],
type: 'foreign key',
name: 'cart_user_id_fkey',
references: {
table: 'users',
field: 'user_id'
}
})
})
])
/**
* Add altering commands here.
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},
我首先添加列,然后将约束添加到表中。当我运行这个时,我收到此错误:
(节点:7106)未经处理的promisreeptiondwarning:sequelizeForeignKeyConstrainTerror:无法添加或更新子行:forefer键约束失败(
bazaah
.#sql-5c24_1325d
,约束cart_user_id_fkey
forefer toursem tofern foright foright forefer键(user_id
)参考(users
) ))为什么在添加列后尝试添加约束失败?
user_id
就您而言,我不知道应该使用什么值作为默认值,因此您唯一的选择就是使其可为空。