[英]How to change datatype of a column in postgresql database using knex migration?
I have a column in a postgresql database table.我在 postgresql 数据库表中有一个列。 Currently it is of datatype INTEGER, I want to change it to JSONB datatype.
目前它的数据类型为 INTEGER,我想将其更改为 JSONB 数据类型。
Maybe this topic can answer your question: Modify column datatype in Knex migration script也许这个话题可以回答你的问题: 修改 Knex 迁移脚本中的列数据类型
For more detail you can have a look at the knex documentation: https://knexjs.org/#Schema-alter有关更多详细信息,您可以查看 knex 文档: https ://knexjs.org/#Schema-alter
Knex supports you to write alter column as the way you create column. Knex 支持您按照创建列的方式编写更改列。 The different is that you have to use alterTable() to get alterTable builder in knex that will support modifing your database information.
不同之处在于您必须使用 alterTable() 在 knex 中获取支持修改数据库信息的 alterTable builder。
Or else you can take a look at my code:或者你可以看看我的代码:
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(); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.