question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow to define database dynamically

See original GitHub issue

Peewee 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:closed
  • Created 10 years ago
  • Comments:26 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
eukreigncommented, Apr 9, 2020

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?

1reaction
jiamocommented, Sep 5, 2020

"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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found