Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you query a table with a schema in Sequelize.js?

I have a table called 'wallets' in the 'dbo' schema of my postgresql database. When I try to query it I get the error:

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

This is my code:

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()

What should I change in the model definition to set the schema name correctly?

like image 498
Richard Avatar asked May 09 '16 07:05

Richard


People also ask

How do I query with Sequelize?

Sequelize instance comes with the query() method which you can use to run a raw query. The syntax of the method is as shown below: const [results, metadata] = await sequelize. query( "Your query here", { options } );

How do you find the table name in Sequelize model?

Detail about Using model Class getTableName()Have schema => return { tableName, schema, delimter } object. Depending on that we can access in one of the way: // model has no schema const tableName = Course. getTableName(); // or // model has schema const tableName = Course.

What is query interface in Sequelize?

An instance of Sequelize uses something called Query Interface to communicate to the database in a dialect-agnostic way. Most of the methods you've learned in this manual are implemented with the help of several methods from the query interface.

Does Sequelize automatically create table?

With this call, Sequelize will automatically perform an SQL query to the database and create a table, printing the message Book table created successfully! . As mentioned, the sync() method is a promise-based method, which means it can also perform error handling.


1 Answers

You can add the schema to the table definition using the options object:

options['schema'] = 'dbo';

I found this by stepping into the query creation code at runtime - it doesn't appear to be in the documentation. I've opened a Sequelize bug to address this: https://github.com/sequelize/sequelize/issues/5864

like image 106
Richard Avatar answered Oct 14 '22 08:10

Richard