简体   繁体   中英

Where do I put and how to use Table() calls in a project with SQLAlchemy Core and flask?

So I want to use SQLAlchemy Core and flask in an application. I have read through the documentation and a lot of tutorials how to structure your application. All of the SQLAlchemy Core tutorials put the Database description directly into the app.py file of the application. I'd like to put this Database description into its own python module (called tables/ here) and usw it in my views, forms etc. So I want to achieve a basic layout like the following:

myapp/
    run.py
    tables/
        __init__.py
        tables1.py
        tables2.py
        ...
    views/
        views1.py
        ...
    ...

What I struggle especially with is the following:

  1. Should I put metadata = MetaData() into tables/__init__.py and import it in the tables/__init__.py files?
  2. Am I supposed to simply import the instances from the tables/tablesX.py files if I want to access my Tables?
  3. The perfect solution for me would be to put all the tables into the metadata object. If I then need access to a table in eg a view I could simply pull the table out of the metadata with table = meta.tables[table_name] . But I have no clue how to pass the metadata object around so that it holds all the tables.
  4. Or am I completly on the wrong path here and am I supposed to create the table description (ie call table = Table(name, tmp_metadata,...) directly in my views.py files?

Thank you for your time and best wishes,

AH

Let's do it one step at a time. Minimal application:

from sqlalchemy import ...
from flask import ...
from flask.ext.sqlalchemy import ...

app = Flask()
db = SQLAlchemy()

class Foo(db.Model):
    ...

@app.route(...)
def foo():
    Foo.query.filter(...)
    return ...

Let's move the db stuff out into a separate module:

# db.py
from sqlalchemy import ...
from flask.ext.sqlalchemy import ...

from .app import app

db = SQLAlchemy(app)

class Foo(db.Model):
    ...

# app.py
from flask import ...

from .db import Foo

app = Flask()

@app.route(...)
def foo():
    Foo.query.filter(...)
    return ...

But wait, we have a circular dependency, so we have to move app out:

# app.py
from flask import *

app = Flask()

# db.py
from sqlalchemy import ...
from flask.ext.sqlalchemy import ...

from .app import app

db = SQLAlchemy(app)

class Foo(db.Model):
    ...

# views.py
from flask import ...

from .db import Foo
from .app import app

@app.route(...)
def foo():
    Foo.query.filter(...)
    return ...

Next, we can split db.py :

# app.py same

# views.py same

# db/__init__.py
from flask.ext.sqlalchemy import ...

from ..app import app
from .foo import Foo

db = SQLAlchemy(app)

# db/foo.py
from sqlalchemy import ...
from . import db

class Foo(db.Model):
    ...

But wait, we have a circular dependency again, so we have to move db out:

# app.py same

# views.py same

# db/__init__.py
from .common import db
from .foo import Foo

# db/common.py
from flask.ext.sqlalchemy import ...

from ..app import app

db = SQLAlchemy(app)

# db/foo.py
from sqlalchemy import ...
from .common import db

class Foo(db.Model):
    ...

SQLAlchemy Core configuration is exactly the same, except replace class Foo with your foo_table = Table(...) declarations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM