Re: [flask] How to use SQLAlchemy declarative and multiple databases
- From:
- Anton
- Date:
- 2011-07-15 @ 15:45
Still cant get it to work. Here is the sample app:
> import os
>
from flask import Flask
> from flaskext.sqlalchemy import SQLAlchemy
> from flaskext.script import Manager
>
app = Flask(__name__)
>
_basedir = os.path.abspath(os.path.dirname(__file__))
>
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' +
> os.path.join(_basedir, 'default.db')
> app.config['SQLALCHEMY_BINDS'] = {
> 'db1':'sqlite:///' + os.path.join(_basedir, 'db1.db'),
> 'db2':'sqlite:///' + os.path.join(_basedir, 'db2.db'),
> }
> app.config['DEBUG'] = True
>
db = SQLAlchemy(app)
> manager = Manager(app)
>
if __name__ == '__main__':
> manager.run()
>>> db.get_engine(app, bind=None)
Engine(sqlite:////home/anton/development/bumerang/src/default.db)
>>> db.get_engine(app, bind='db1')
Engine(sqlite:////home/anton/development/bumerang/src/default.db)
>>> db.get_engine(app, bind='db2')
Engine(sqlite:////home/anton/development/bumerang/src/default.db)
>>> db.create_all()
creates only default.db
What i am doing wrong?
2011/7/15 Anton <anton.ponadiozin@gmail.com>
> My fault, i was using the example from
> http://flask.pocoo.org/docs/patterns/sqlalchemy/ and it doesn't use
> Flask-SQLAlchemy.
> Thanks!
>
> 2011/7/15 Armin Ronacher <armin.ronacher@active-4.com>
>
>> Hi,
>>
>> On 7/15/11 2:37 PM, Anton wrote:
>> > Is there a "normal" way to use SQLAlchemy declarative and multiple
>> > databases?
>> > The only way i see it, is just create another instance of db, but it
>> > seems dull.
>> Flask-SQLAlchemy supports that with bind keys. If you are using
>> SQLAlchemy without the Flask extension look at how Flask-SQLAlchemy does
>> that and adapt your code.
>>
>> http://packages.python.org/Flask-SQLAlchemy/binds.html
>>
>>
>> Regards,
>> Armin
>>
>>
>
Re: [flask] How to use SQLAlchemy declarative and multiple databases
- From:
- Armin Ronacher
- Date:
- 2011-07-15 @ 15:57
Hi,
On 7/15/11 5:45 PM, Anton wrote:
> What i am doing wrong?
It will only generate something if models or tables are defined for the
db. That particular script does not import or declare any models, so
the binds will never be marked as used.
Regards,
Armin
Re: [flask] How to use SQLAlchemy declarative and multiple databases
- From:
- Armin Ronacher
- Date:
- 2011-07-15 @ 16:06
Hi,
Oh wow. That actually is a bug *and* a broken testcase. The testcase
did not show this because it used in-memory database connections. I
will fix this right away.
Regards,
Armin
Re: [flask] How to use SQLAlchemy declarative and multiple databases
- From:
- Armin Ronacher
- Date:
- 2011-07-15 @ 16:16
Hi,
Flask-SQLAlchemy 0.13 fixes this. pip install -U Flask-SQLAlchemy
Regards,
Armin
Re: [flask] How to use SQLAlchemy declarative and multiple databases
- From:
- Anton
- Date:
- 2011-07-15 @ 18:40
Thanks Armin!
2011/7/15 Armin Ronacher <armin.ronacher@active-4.com>
> Hi,
>
> Flask-SQLAlchemy 0.13 fixes this. pip install -U Flask-SQLAlchemy
>
>
> Regards,
> Armin
>