Knex.js 中的迁移 - 无法在可空字段上设置外键
Migration in Knex.js - cannot set foreign key on nullable field
我有一个 table,我目前有一个必需的外键约束。我需要将该约束设为可选,因此如果值为 null 则没有约束,否则它应该强制执行该约束。
我正在 Knex.js 中编写迁移,并且我有以下迁移。 3 个语句中的每一个都是独立工作的,但是如果使该列可为空,我将无法重新添加外键。
exports.up = knex => {
return knex.schema
// Statement 1
.table("waypoints", table => {
table.dropForeign("port_id");
})
// Statement 2
.alterTable("waypoints", table => {
table
.integer("port_id")
.nullable()
.alter();
})
// Statement 3
.table("waypoints", table => {
table.foreign("port_id").references("port.id");
});
};
如何将此列设为可选?
最终使用 knex.raw()
:
exports.up = knex => {
return knex.raw(
"ALTER TABLE `waypoints` CHANGE `suggested_route_id` `suggested_route_id` INT(10) UNSIGNED NULL;"
);
};
exports.down = knex => {
return knex.raw(
"ALTER TABLE `waypoints` CHANGE `suggested_route_id` `suggested_route_id` INT(10) UNSIGNED NOT NULL;"
);
};
找不到通过 Knex 执行此操作的方法 API。
看起来很像 https://github.com/knex/knex/issues/1218#issuecomment-650614036
exports.up = knex => {
return knex.schema.alterTable('waypoints', table => {
table.integer('suggested_route_id').nullable().alter();
});
};
exports.down = knex => {
return knex.schema.alterTable('waypoints', table => {
// CAVEAT: if you have waypoints records where `suggested_route_id` is `null` when you run this, how should those be migrated to respect the non-nullable constraint?
// One way is to manually assign a value before running the rollback migration
// Or you could also remove the associated records depending on your use-case.
table.integer('suggested_route_id').notNullable().alter();
});
};
我有一个 table,我目前有一个必需的外键约束。我需要将该约束设为可选,因此如果值为 null 则没有约束,否则它应该强制执行该约束。
我正在 Knex.js 中编写迁移,并且我有以下迁移。 3 个语句中的每一个都是独立工作的,但是如果使该列可为空,我将无法重新添加外键。
exports.up = knex => {
return knex.schema
// Statement 1
.table("waypoints", table => {
table.dropForeign("port_id");
})
// Statement 2
.alterTable("waypoints", table => {
table
.integer("port_id")
.nullable()
.alter();
})
// Statement 3
.table("waypoints", table => {
table.foreign("port_id").references("port.id");
});
};
如何将此列设为可选?
最终使用 knex.raw()
:
exports.up = knex => {
return knex.raw(
"ALTER TABLE `waypoints` CHANGE `suggested_route_id` `suggested_route_id` INT(10) UNSIGNED NULL;"
);
};
exports.down = knex => {
return knex.raw(
"ALTER TABLE `waypoints` CHANGE `suggested_route_id` `suggested_route_id` INT(10) UNSIGNED NOT NULL;"
);
};
找不到通过 Knex 执行此操作的方法 API。
看起来很像 https://github.com/knex/knex/issues/1218#issuecomment-650614036
exports.up = knex => {
return knex.schema.alterTable('waypoints', table => {
table.integer('suggested_route_id').nullable().alter();
});
};
exports.down = knex => {
return knex.schema.alterTable('waypoints', table => {
// CAVEAT: if you have waypoints records where `suggested_route_id` is `null` when you run this, how should those be migrated to respect the non-nullable constraint?
// One way is to manually assign a value before running the rollback migration
// Or you could also remove the associated records depending on your use-case.
table.integer('suggested_route_id').notNullable().alter();
});
};