需要使用MySql更改MySql Db中列的数据类型和默认值;之后,数据类型为date,需要将其更改为dateTime,还需要将从NULL更改为的默认值更改为dateTime。
下面给出了实现MySql查询的相应方法
ALTER TABLE `Employees`
CHANGE COLUMN `added_date` `added_date` DATETIME DEFAULT CURRENT_TIMESTAMP ;我已经创建了knex迁移文件来运行上面的更改:
迁移文件的内容如下:
exports.up = function(knex, Promise) {
return knex.schema.alterTable('Employee', function(t) {
t.dateTime('added_date').defaultTo(knex.fn.now());
});
} ;
exports.down = function(knex, Promise) {
return knex.schema.alterTable('Employee', function(t) {
t.date('added_date').nullable();
});
};但这总是在构建过程中抛出错误。就像
警告-迁移失败的错误: alter Employee添加added_date日期时间默认CURRENT_TIMESTAMP - ER_DUP_FIELDNAME:重复列名'added_date‘在迁移最新版本错误: ER_DUP_FIELDNAME:重复列名'added_date’
任何人都可以共享更改表的确切方法/语法吗?
发布于 2017-01-31 19:07:23
目前(KNEX0.12.6)没有办法使用out raw()调用来完成这一任务。将来,如果https://github.com/tgriesser/knex/pull/1759完成了,那么可以使用更复杂的列更改函数。
exports.up = function(knex, Promise) {
return knex.schema.raw('ALTER TABLE `Employees` CHANGE COLUMN `added_date` `added_date` DATETIME DEFAULT CURRENT_TIMESTAMP');
};
exports.down = function(knex, Promise) {
return knex.schema.raw('ALTER TABLE `Employees` CHANGE COLUMN `added_date` `added_date` DATE DEFAULT NULL');
};编辑:我开始完成这个拉请求,在下一个版本(0.12.7或0.13.0)中,可以这样做:
exports.up = function(knex, Promise) {
return knex.schema.alterTable('Employee', function(t) {
t.dateTime('added_date').defaultTo(knex.fn.now()).alter();
});
};
exports.down = function(knex, Promise) {
return knex.schema.alterTable('Employee', function(t) {
t.date('added_date').nullable().alter();
});
};我还将把这些添加到集成测试中,以确保它们正常工作。
https://stackoverflow.com/questions/41959299
复制相似问题