如何在Sequelize.js中查询具有模式的表?

时间:2016-05-09 07:44:00

标签: postgresql sequelize.js

我有一张名为' wallets'在'dbo'我的postgresql数据库的架构。 当我尝试查询它时,我收到错误:

Executing (default): SELECT "wallets_id", "confirmed_balance", "unconfirmed_balance", "created_at", "updated_at" FROM "dbo.wallets" AS "dbo.wallets";
Unhandled rejection SequelizeDatabaseError: relation "dbo.wallets" does not exist

这是我的代码:

get_dbo__wallets() : any {
    const options = this.getDatabaseDefaultOptions('dbo.wallets');
    const entity : types.IObjectWithStringKey = {};
    entity['wallets_id'] = {type: Sequelize.UUID, primaryKey: true, allowNull : false};
    entity['confirmed_balance'] = Sequelize.BIGINT;
    entity['unconfirmed_balance'] = Sequelize.BIGINT;
    return this._db.define('dbo.wallets', entity, options);
}

getDatabaseDefaultOptions(tableName : string) : types.IObjectWithStringKey {
    const options : types.IObjectWithStringKey = {};
    options['timestamps'] = true;
    options['createdAt'] = 'created_at';
    options['updatedAt'] = 'updated_at';
    options['underscored'] = false;

    options['paranoid'] = false;
    options['deletedAt'] = false;

    options['freezeTableName'] = true;
    options['tableName'] = tableName;
    return options;
}

//and then I call:  get_dbo__wallets().all()

我应该在模型定义中更改哪些内容才能正确设置架构名称?

1 个答案:

答案 0 :(得分:5)

您可以使用选项对象将架构添加到表定义中:

options['schema'] = 'dbo';

我通过在运行时插入查询创建代码找到了这一点 - 它似乎不在文档中。我已经打开了一个Sequelize错误来解决这个问题:https://github.com/sequelize/sequelize/issues/5864