【发布时间】:2021-07-19 05:20:00
【问题描述】:
我在 postgresql 数据库表中有一个列。目前是INTEGER数据类型,我想改成JSONB数据类型。
【问题讨论】:
标签: sql reactjs postgresql knex.js
我在 postgresql 数据库表中有一个列。目前是INTEGER数据类型,我想改成JSONB数据类型。
【问题讨论】:
标签: sql reactjs postgresql knex.js
也许这个话题可以回答你的问题:Modify column datatype in Knex migration script
有关更多详细信息,您可以查看 knex 文档:https://knexjs.org/#Schema-alter
Knex 支持您以创建列的方式编写更改列。不同的是,您必须使用 alterTable() 来获取 knex 中的 alterTable builder,该构建器将支持修改您的数据库信息。
或者你可以看看我的代码:
export function up(knex) {
return knex.schema.createTable('users', table => {
table.increments('id').primary('id');
table.string('username').notNullable().unique('username');
table.string('fullName');
table.string('email');
table.string('password');
table.timestamps(true, true);
});
}
export async function up(knex) {
const transaction = await knex.transaction();
try {
await transaction.schema.alterTable('users', table => {
table.string('email').notNullable().alter();
});
await transaction.commit();
} catch (error) {
await transaction.rollback();
}
}
【讨论】: