ezg*_*ezg 6 javascript postgresql backend node.js knex.js
我正在使用 Node.js 和 Knex 为我的路由器构建服务。但是,我无法弄清楚如何向现有表中添加一列,任何帮助将不胜感激。另外,我正在使用 PostgreSQL,但我认为这对这个问题并不重要。
所以,这就是我向表中添加行的方法:
insertData(knex, table, row) {
return knex
.insert(row)
.into(table)
.returning('*')
.then(rows => {
return rows[0];
});
}
Run Code Online (Sandbox Code Playgroud)
我猜在表中添加一列会与此类似吗?我只是无法弄清楚/找到解决方案。
Isa*_*Pak 26
对于迁移:
这是摘自这篇文章的
knex migrate:make add_new_column_to_table
exports.up = function(knex) {
return knex.schema.table('<table name>', table => {
table.string('<new column name>', 128);
})
};
exports.down = function(knex) {
return knex.schema.table('<table name>', table => {
table.dropColumn('<new column name>');
})
};
Run Code Online (Sandbox Code Playgroud)
knex migrate:latest
小智 6
上面的答案是正确的,除了...
请务必写上“knex.schema。ALTERTABLE ”,而不是“knex.schema.table”。
下面是正确的:
return knex.schema.alterTable('<table name>', table => {
table.dropColumn('<new column name>');
})
Run Code Online (Sandbox Code Playgroud)
您应该使用 Knex.js 提供的架构构建功能
以下是其官方文档中的示例:
//Chooses a database table, and then modifies the table
knex.schema.table('users', function (table) {
table.string('first_name');
table.string('last_name');
})
//Outputs:
//alter table `users` add `first_name` varchar(255), add `last_name` varchar(255);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9023 次 |
最近记录: |