Closed petercpwong closed 6 years ago
Providing schema like that isn't supported by knex and therefore isn't supported by objection. Objection still has a limited support for the things that do work with knex, but as long as knex doesn't support giving schema as a part of the reference, objection cannot really guarantee support or implement most of the things. If you do figure out a simple and clean way to support this use case, I can consider merging a PR.
Knex does support schemas through the withSchema
method. I haven't tried this, but maybe you could do something like this:
Create a shared base class
class SchemaModel extends Model {
// Override the static `query` method to add a schema to each query
static query(...args) {
const query = super.query(...args)
if (this.schemaName) {
query.withSchema(this.schemaName)
}
return query
}
}
Inherit all your models from that class
class SomeModel exends SchemaModel {
static tableName() {
return 'table_a'
}
static schemaName() {
return 'myschema'
}
}
@petercpwong see related multi-schema issues: 1.2.0 changes ManyToMany/HasOneThrough join SQL Multi-schema Relationships [PostgreSQL] add multi-schema integration tests for Postgres
As a note, I tried using schemaName
as koskimas suggested, but that did not work for me in cross-schema JOIN
s. So I currently use tableName() { return 'mySchema.tableName' }
.
Current workaround is to version-lock to 1.1.10
while a fix is thought of...
Actually this is a duplicate of #1007
Running a query with
eager()
on aHasOneThroughRelation
that has a table name prefixed with a schema will yield an SQL error:Here's a sample of what the SQL looks like:
Notice the problem is in the alias of the join table:
The table alias
"myschema.table_b"
should be dropped since it's referenced asmyschema"."table_b"
everywhere else in the statement.Steps to reproduce:
HasOneThroughRelation
(ModelA -> JoinTable -> ModelB)tableName
that contains a schema (ie.myschema.table_a
)eager
orloadRelated
with the relation