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?
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 } );
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With