I have 2 schemas on my db:schema1
and public
and I want to create some tables there. I tried code like this in my migration:
return knex.schema
.raw('CREATE SCHEMA IF NOT EXISTS schema1;')
.createTableIfNotExists('table1', table => {
table.increments('id')
})
.withSchema('public')
.createTableIfNotExists('table2', table => {
table.increments('id')
})
And I hoped to have: schema1.table1
and public.table2
but I don't. Any ideas how to do that?
Belayer is quite correct in the comments, but I'll expand into an answer to demonstrate the syntax. You'll also need to avoid doing things like:
createTableIfNotExists('schema1.table1', table => {
per Identifier Syntax, this won't work: just get into the habit of always using withSchema
.
Further, I don't recommend issuing subsequent createTable
s without waiting for the previous one to finish. While it might work, I think you'll run into trouble with relations etc. That makes your original code more like:
exports.up = knex =>
knex
.raw("CREATE SCHEMA IF NOT EXISTS schema1")
.then(() =>
knex.withSchema("schema1").createTableIfNotExists("table1", table => {
table.increments("id");
})
)
.then(() =>
knex.withSchema("public").createTableIfNotExists("table2", table => {
table.increments("id");
})
);