Dynamically switching between databases. #5564
Unanswered
timnewsham
asked this question in
Q&A
Replies: 2 comments 3 replies
-
Hi @timnewsham Maybe you can use https://gorm.io/docs/scopes.html#Dynamically-Table |
Beta Was this translation helpful? Give feedback.
3 replies
-
Hi @jinzhu , but this solution will fix the Preload tables? I have a problem trying a similar method, that fix the name of the "principal" table but not the tables queried by the use of preload. There is a way to change the tablename dynamically of the preload table? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to build GORM queries that use a single database pool but dynamically select between different databases running on the same database server (postgresql in this case). Looking around I found a few solutions:
use DBNAME
. This seems to require that I give up a lot of the benefits of using GORM to construct my database accesses.Table("db_name.table_name")
construct to explicitly pick the database name and database table for each database access. This is much better, but I have to give up the automatic table name selection features of GORM.Is there an easier way to do this that would let me specify just the db_name but allow GORM to still dynamically pick the table_name in the normal way (ie. based off of the model object that is being accessed)?
Ideally I would be able to use the chainable API to add a constraint that fixes the db_name without explicitly choosing the table name.
Tim
Beta Was this translation helpful? Give feedback.
All reactions