【问题标题】:Sequelize Migration Remove Primary KeySequelize 迁移删除主键
【发布时间】:2016-11-15 17:13:17
【问题描述】:

我正在阅读 sequelize 文档,我不确定是否有办法删除列上的主键约束。

这是最相关的链接: http://docs.sequelizejs.com/en/latest/docs/migrations/#changecolumntablename-attributename-datatypeoroptions-options

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    对我有用的是使用 sql,不确定我是否做错了什么,但我尝试过的其他方法似乎都不起作用。

    发件人:https://github.com/sequelize/sequelize/issues/313

    up: function (queryInterface, Sequelize) {
        return queryInterface.sequelize.query('ALTER TABLE my_table DROP PRIMARY KEY');
    }
    

    【讨论】:

    • 注意:这仅适用于MySql
    【解决方案2】:

    是的,使用 changeColumn 并提供一个完整的新列定义(与原始列相同),但没有主键。

    【讨论】:

      【解决方案3】:

      以下代码将删除表的主键:

      up: function (queryInterface, Sequelize) {
          return queryInterface.removeConstraint('table_name', 'PRIMARY');
      }
      

      这消除了外键之类的约束:

      up: function (queryInterface, Sequelize) {
          return queryInterface.removeConstraint('table_name', 'constraint_name');
      }
      

      【讨论】:

        【解决方案4】:

        示例代码:

        'use strict';
        
        module.exports = {
          up: (queryInterface, Sequelize) => {
            return queryInterface.removeConstraint('myTable', 'myTable_pkey'); // myTable_pkey = name of constraint
          },
        
          down: (queryInterface, Sequelize) => {
            return queryInterface.addConstraint('myTable', {
              fields: ['id'],
              type: 'primary key',
              name: 'myTable_pkey'
            });
          }
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-26
          • 2020-05-07
          • 1970-01-01
          • 2019-01-22
          • 2019-01-29
          相关资源
          最近更新 更多