代码之家  ›  专栏  ›  技术社区  ›  Martin Nordström

在不丢失数据的情况下更新knex.js表

  •  1
  • Martin Nordström  · 技术社区  · 6 年前

    我不确定如何用knex.js更改模式中的表。目前我有一张这样的桌子:

    .createTable('users', function(table) {
      table.increments('id')
      table
        .text('username')
        .notNullable()
        .unique()
      table.text('name')
      table
        .text('email')
        .notNullable()
        .unique()
      table.string('password').notNullable()
      table
        .text('profile_image')
        .defaultTo('http://www.ecehh.org/wp-content/uploads/2018/02/avatar.jpg')
    

    我想做的是改变 defaultTo profile_image . 我从这里读到的 http://perkframework.com/v1/guides/database-migrations-knex.html 那个 “我们从不希望在运行迁移文件之后对其进行编辑,因为运行knex migrate:latest knex时不会进行更改。迁移将只运行一次。” 因此,我想知道如何在不重新运行迁移的情况下更新它的值,然后释放所有当前数据。

    谢谢你的阅读!

    1 回复  |  直到 6 年前
        1
  •  1
  •   paulogdm    6 年前

    它是生产服务器吗?

    从这个 issue 我想这行得通。

    exports.up = knex => {
      return knex.schema
        .alterTable('users', table => {
          table.text('profile_image').defaultTo('myurl').alter()
      });
    };
    
    exports.down = knex => {
      return knex.schema.alterTable('users', table => {
          table.text('profile_image').defaultTo('myurl').alter()
      });
    };