Flask-Sqlalchemy多个数据库(绑定)与相同的模型(DB.Model类) [英] Flask-Sqlalchemy multiple databases (binds) with the same model (class DB.Model)

查看:895
本文介绍了Flask-Sqlalchemy多个数据库(绑定)与相同的模型(DB.Model类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想用Flask-SQLAlchemy创建一个带有Flask的API,如下所示:以下内容:
$ b $ ol

  • 一个用户/密码的sqlite数据库


    SQLALCHEMY_DATABASE_URI ='sqlite:////path/to/users.db'

      class User(DB。模型):
    __tablename__ ='users'
    id = DB.Column(DB.Integer,primary_key = True)
    username = DB.Column(DB.String(64),index = True )
    password = DB.Column(DB.String(128))




    1. 比方说,我有多个客户,用户可以创建使用



      $ http POST http:// localhost:5000 / api / customers / name = customer1



      类CustomerModel(DB.Model):

        __ tablename__ ='customer'
      customer_id = DB.Column(DB.Integer,primary_key = True)
      customer_name = DB.Column(DB.String,unique = True,index = True)
      <我需要创建一个单独的sqlite文件(例如:customer1.db)

      $ b

    2. $ b

      对于每个客户,所以我会:

        SQLALCHEMY_BINDS = {
      'customer1'='sqlite:// //path/customer1.db',
      'customer2'='sqlite:////path/customer2.db',
      ...
      }

      我的问题是:


      1. 我没有固定数量的客户,因此我无法为每个客户创建一个模型类,并为每个客户指定 bind_key 。是否有可能使用Flask-SQLAlchemy来做到这一点,或者我需要使用普通的SQLAlchemy?

      2. 可以说我在data /中有3个customers作为customer1。 db,customer2.db和customer3.db。
        我将启动应用程序并创建SQLALCHEMY_BINDS字典,其中列出了data /中的文件,然后是DB.create_all()。


      对特定客户的请求如何使用Flask-SQLAlchemy DB.session绑定到正确的.db文件?我已经看到了这一点,但没有答案,我找不到办法。 在Flask-SQLAlchemy的相同类中使用不同的绑定

      解决方案


      $为什么要为每个客户完全分离DB文件? b $ b

      在任何情况下,使用直接的SQLAlchemy都更容易。你可以创建一个getter函数,返回一个指向你的db文件的会话。

        def get_session(customer_id):
      sqlite_url ='sqlite:////path/customer%s.db'%customer_id
      engine = create_engine(sqlite_url)

      #初始化数据库,如果尚未初始化
      Base.metadata.create_all(引擎)

      Session = sessionmaker(bind =引擎)
      session = Session()

      返回会话

      然后您可以使用并关闭该会话。

      但在不知道具体用例的情况下,很难理解为什么要这样做,而不是仅仅使用一个SQLite数据库。


      I'm a beginner with python/Flask/SQLAlchemy so sorry if my questions are dumb.

      I want to create an API with Flask using Flask-SQLAlchemy as following:

      1. one sqlite database for users/passwords

      SQLALCHEMY_DATABASE_URI = 'sqlite:////path/to/users.db'

       class User(DB.Model):
          __tablename__ = 'users'
          id = DB.Column(DB.Integer, primary_key=True)
          username = DB.Column(DB.String(64), index=True)
          password = DB.Column(DB.String(128))
      

      1. Lets say I have multiple "customers" witch a user can create using

        $ http POST http://localhost:5000/api/customers/ name=customer1

        I would have a model like this:

        class CustomerModel(DB.Model):

        __tablename__ = 'customer'
        customer_id = DB.Column(DB.Integer, primary_key=True)
        customer_name = DB.Column(DB.String, unique=True, index=True)
        

      I need to create a separate sqlite file (example: customer1.db) for each "customers" so I would have:

      SQLALCHEMY_BINDS = {
          'customer1' = 'sqlite:////path/customer1.db',
          'customer2' = 'sqlite:////path/customer2.db',
          ...
      }
      

      My questions are:

      1. I do not have fixed number of "customers" so I cannot create a model class for each and specify the "bind_key" for each. Is it possible to do this with Flask-SQLAlchemy or I need to use plain SQLAlchemy?

      2. lets say I have 3 "customers" in data/ as customer1.db, customer2.db and customer3.db. I would start the application and create SQLALCHEMY_BINDS dictionary listing the files in data/ and then DB.create_all().

      on a request for a specific "customer" how can I bind to the right .db file using the Flask-SQLAlchemy DB.session? I've seen this but there is no answer and I cannot find the way to do it. Using different binds in the same class in Flask-SQLAlchemy

      解决方案

      Why exactly do you want entirely separate DB files for each customer?

      In any case this is easier with straight SQLAlchemy. You can create a getter function which returns a session pointing to your db file.

      def get_session(customer_id):
          sqlite_url = 'sqlite:////path/customer%s.db' % customer_id
          engine = create_engine(sqlite_url)
      
          # initialize the db if it hasn't yet been initialized
          Base.metadata.create_all(engine)
      
          Session = sessionmaker(bind=engine)
          session = Session()
      
          return session
      

      You can then use and close that session.

      But without knowing your specific use case, it is difficult to understand why you would want to do this instead of just using a single SQLite database.

      这篇关于Flask-Sqlalchemy多个数据库(绑定)与相同的模型(DB.Model类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

      查看全文
  • 登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆