简体   繁体   English

Knex / Bookshelf:如何在所有查询/命令中使用Postgres架构

[英]Knex/Bookshelf: How to Use Postgres Schema in All Queries/Commands

I am writing an application with Knex, Bookshelf, and Postgres. 我正在用Knex,Bookshelf和Postgres编写一个应用程序。 I would like all Postgres commands issued from my application to be on tables in one schema. 我希望从我的应用程序发出的所有Postgres命令都在一个模式中的表上。 Is there a way I can accomplish this without specifying .withSchema('my_schema_name') for each individual query/command? 有没有一种方法可以实现这一点,而无需为每个单独的查询/命令指定.withSchema('my_schema_name')

Ideally, I could specify the schema in the knexfile.js, but I don't see a way to do this. 理想情况下,我可以在knexfile.js中指定架构,但我没有看到这样做的方法。

I have already tried: 我已经尝试过了:

  • prepending the table name with 'my_schema_name.' 在表名前加上'my_schema_name'。
  • issuing a USE SCHEMA command at the launch of my application 在我的应用程序启动时发出USE SCHEMA命令

Both of these resulted in various errors. 这两个都导致了各种错误。

The solution was actually to pretend the tableName with 'my_schema.'. 解决方案实际上是使用'my_schema。'伪装tableName。 (Notice the dot) (注意点)

The error I had seen when I tried this initially was due to something else. 我最初尝试这个时遇到的错误是由于其他原因造成的。

Your connection should call SET search_path TO my_schema, public; 您的连接应该调用SET search_path TO my_schema, public; after being initialized. 初始化后。 This is a connection (session) property and it should do what you are asking for. 这是一个连接(会话)属性,它应该按照您的要求执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM