John O
John O

Reputation: 5423

Inserts with explicit schemas in Knex?

I have a simple insert that must occur on a table in a particular schema that won't be in the search path:

db('tick').insert({bigbang: 435114000000, history: 0})
  .then(() => {
     console.log('are we here yet?');
  })
  .catch((err) => {
     console.log(err);
  });

This fails with the error telling me that the relation "tick" does not exist... and it doesn't in whatever the default search_path is.

However, I cannot find any examples or tutorials that explain how to explicitly state which schema to use. I assume that most patterns would just set the search path so as to avoid the necessity of doing so... but I will need to switch between identically named tables in multiple schemas. I've been through the Knex documents multiple times, and for the life of me it looks as if the Schema Builder functionality is only for ddl. Many examples of creating and altering objects, but none with an insert or an update.

Is this possible?

Upvotes: 0

Views: 2215

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84687

From the docs:

withSchema.withSchema([schemaName]) Specifies the schema to be used as prefix of table name.

knex.withSchema('public').select('*').from('users')

Upvotes: 1

Related Questions