向现有数据库添加新列 table
Add new column to existing database table
我已经使用 node ace make:migration user
和 node ace make:model user
创建了模型和迁移,现在我想向用户模型添加另一列。
如何在已经创建模型并迁移后添加另一个新列?
这里是关于迁移的 KnexJS 文档:https://knexjs.org/#Schema-Building
AdonisJS 文档:https://docs.adonisjs.com/guides/database/migrations#alter-example
如何创建新列
创建新的迁移文件
node ace make:migration add_new_column --table=users
更新迁移文件中的代码
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
export default class Users extends BaseSchema {
protected tableName = 'users'
public async up () {
this.schema.table(this.tableName, (table) => {
// Create new column with table.<type>(<name>)
table.text('my_new_column')
})
}
public async down () {
this.schema.table(this.tableName, (table) => {
table.dropColumn('my_new_column')
})
}
}
我已经使用 node ace make:migration user
和 node ace make:model user
创建了模型和迁移,现在我想向用户模型添加另一列。
如何在已经创建模型并迁移后添加另一个新列?
这里是关于迁移的 KnexJS 文档:https://knexjs.org/#Schema-Building
AdonisJS 文档:https://docs.adonisjs.com/guides/database/migrations#alter-example
如何创建新列
创建新的迁移文件
node ace make:migration add_new_column --table=users
更新迁移文件中的代码
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
export default class Users extends BaseSchema {
protected tableName = 'users'
public async up () {
this.schema.table(this.tableName, (table) => {
// Create new column with table.<type>(<name>)
table.text('my_new_column')
})
}
public async down () {
this.schema.table(this.tableName, (table) => {
table.dropColumn('my_new_column')
})
}
}