Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask-sqlalchemy - PostgreSQL - Define specific schema for table?

I want to define a specific schema for a 'model' using flask-sqlalchemy. When you create a table object in sqlalchemy itself it has a parameter to pass for schema name.

How do I do this in flask-sqlalchemy?

like image 263
jbaranski Avatar asked Aug 08 '12 22:08

jbaranski


2 Answers

When you define your model class use:

__table_args__ = {"schema":"schema_name"}

maybe it will save someone else some hunting.

like image 174
jbaranski Avatar answered Nov 14 '22 16:11

jbaranski


For future references:

db = flask.ext.sqlalchemy.SQLAlchemy(app)

app.config['SQLALCHEMY_DATABASE_URI'] = 'your_default_schema_db_uri'
app.config['SQLALCHEMY_BINDS'] = {'other_schema': 'your_other_db_uri'}

class TableA(db.Model):
    # This belongs to Default schema, it doesn't need specify __bind_key__
    ...

class TableB(db.Model):
      # This belongs to other_schema
    __bind_key__ = 'other_schema'
    ...
like image 21
Manuel Lopera Avatar answered Nov 14 '22 18:11

Manuel Lopera