我想给我的银行模型定义添加一个与postrgres表定义匹配的约束。
我已尝试使用queryInterface来执行此操作,但我有一个queryInterface未定义错误。
以下是我的银行模型定义:

const Banks = db.define('banks', {
    bank_name: {
        type: Sequelize.STRING,
        allowNull: false
    },
    floor_number: {
        type: Sequelize.STRING,
        allowNull: false
    },
    bank_col_num: {
        type: Sequelize.INTEGER,
        allowNull: false
    },
    bank_row_num: {
        type: Sequelize.INTEGER,
        allowNull: false
    },
    bank_orientation: {
        type:Sequelize.CHAR
    }

}).then(
     queryInterface.addConstraint('Banks', ['bank_orientation'], {
        type: 'check',
        where: {
             bank_orientation: ['horizontal', 'vertical']
        }
    })
)


以下是我的银行sql模型定义:
CREATE TABLE banks (
    id SERIAL,
    bank_name VARCHAR(50) NOT NULL,
    floor_number VARCHAR(50) NOT NULL,
    bank_col_num INTEGER NOT NULL,
    bank_row_num INTEGER NOT NULL,
    bank_orientation CHAR NOT NULL CHECK (bank_orientation IN ('horizontal', 'vertical')),

    PRIMARY KEY (id)
);

我希望模特能进入我的postgres表

最佳答案

我没有测试下面的语句,但我重新格式化了.then语句。我还在db之前添加了一个queryInterface,因为(queryInterface)在您的示例中没有定义。

const Banks = db.define('banks', {
    bank_name: {
        type: Sequelize.STRING,
        allowNull: false
    },
    floor_number: {
        type: Sequelize.STRING,
        allowNull: false
    },
    bank_col_num: {
        type: Sequelize.INTEGER,
        allowNull: false
    },
    bank_row_num: {
        type: Sequelize.INTEGER,
        allowNull: false
    },
    bank_orientation: {
        type:Sequelize.CHAR
    }

}).then(() => {
    return db.queryInterface.addConstraint('Banks', ['bank_orientation'], {
        type: 'check',
        where: {
             bank_orientation: ['horizontal', 'vertical']
        }
    })
})

10-01 14:43