Allow to define database dynamically
See original GitHub issuePeewee is awesome 👍 😃
I’m using Peewee in my Tornado-powered project and I need to define database connection dynamically, like that:
import tornado
from peewee import *
class Application(tornado.web.Application):
def __init__(self, **kwargs):
# Some init stuff ...
# Setup DB and Models
self.database = PostgresqlDatabase('mydb', user='postgres')
import myapp.users.models as users
self.User = self._get_model(users.User, self.database)
# etc...
def _get_model(self, model, db):
model._meta.database = db
model.create_table(True)
return model
So, I’m using private undocumented _meta
property and I’m not feeling ok about that and I’m not sure what is the best design decision for that.
Anybody have ideas?
Issue Analytics
- State:
- Created 10 years ago
- Comments:26 (11 by maintainers)
Top Results From Across the Web
Example of a Database Configuration Dynamic Application
This section will walk you through the following steps in creating a Database Dynamic Application: Defining the Basic Properties for the Dynamic Application ......
Read more >How to let user dynamically specify database provider and ...
In a perfect world, I would like the app on first run to present the user with a dialog that lets them choose...
Read more >8 Dynamic SQL
In addition, dynamic SQL lets you execute SQL statements that are not supported in static SQL programs, such as data definition language (DDL)...
Read more >Using Variables in Dynamic SQL - SQLShack
In this blog post, you are going to see step by step how to use variables in Dynamic SQL which are extremely useful...
Read more >Switching between databases with dynamic SQL
So no, you cannot dynamically change the current database. If you need to do something like this, you will need to execute any...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Is
bind_ctx
thread safe? Can I bind the same model to different databases in separate threads?If it’s possible can you point me to how this works? From my investigation it seems that
bind_ctx
just monkey patches the class attributes with which database to use so if you set it in one thread you’d overwrite whatever it was set to in another thread, is my understanding correct?"I believe that the vast, vast majority of people use peewee with a single database. "
Yes. But still some situation for multi database.
Some application create db for every user. All db have same table. When different user request api. Using
bind_ctx
in one thread the model will change at runtime. And it don’t have solution but only use lock to bind model to one db.If we have session concept , One thread have multi session. different session the model using different database. That will be very helpful.