【问题标题】:How can I set schema for all models at once at connection time for Sequelize Postgres?如何在 Sequelize Postgres 的连接时一次为所有模型设置架构?
【发布时间】:2020-01-23 23:54:32
【问题描述】:

我可以使用

为临时查询设置架构
sequelize.query(`
    SET SCHEMA 'schema_for_client_name'
`);

我还可以为单个模型设置架构

User.init(
    {
      userId: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true,
      },
      email: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      firstName: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      lastName: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      password: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      salt: {
        type: DataTypes.STRING,
        allowNull: false,
      },
    },
    {
      sequelize,
      tableName: 'users',
      schema: 'schema_for_client_name',
      timestamps: false,
    }
);

是否可以在连接时同时为所有模型设置架构?

【问题讨论】:

    标签: postgresql sequelize.js


    【解决方案1】:

    根据the docsSequelize 构造函数采用可选的默认schema 选项:

    const sequelize = new Sequelize(database, username, password, {schema: 'schema_for_client_name'});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      相关资源
      最近更新 更多