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:
- 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))
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:
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?
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