本文介绍了Flask Sqlalchemy:不同模块之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Flask-SQLAlchemy教程.我在python 2.6上有Flask 0.9,sqlalchemy 0.7.8和flask-sqlalchemy 0.16.

I'm following the Flask-SQLAlchemy tutorial.I have Flask 0.9, sqlalchemy 0.7.8 and flask-sqlalchemy 0.16 on python 2.6.

我正在尝试建立一对多"的关系,就像在他们的教程中一样.

I'm trying to create a "one to many" relationship, like in their tutorial.

class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    addresses = db.relationship('Address', backref='person',
                                lazy='dynamic')

class Address(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(50))
    person_id = db.Column(db.Integer, db.ForeignKey('person.id'))

之后,我可以创建数据库:

After that, I can create the database :

from DataBase.Tables.MyClass import db
db.create_all()

当两个类都在同一文件上创建时,效果很好.

It works well when both classes are created on the same file.

当我想通过2个不同的文件(2个不同的模块)创建此文件时,它将不再起作用.

It does not work anymore when I want to create this through 2 different files (2 different modules).

这只是一个例子(我正在尝试使用很多类来做更复杂的事情,我需要在2个不同的模块之间存在这种关系,但是我会简化它,所以我的问题是会更容易理解.)

This is just an example (I'm trying to do something much more complicated with plenty of classes and I need the relationship to exist between 2 different modules but I'll simplify it so my question can be easier to understand.)

我有2个模块:人"和地址",两个都有:

I have 2 modules : Person and Address, both of them have :

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///C:\\MyBase\\Base.sqlite'
db = SQLAlchemy(app)

并且每个人都有之前编写的类的声明.

and each of them has the declaration of the class written before.

我的主要功能在第三个模块中:

My main function is in a 3rd module:

from DataBase.Tables.Person import db as person_db
from DataBase.Tables.Address import db as address_db

if __name__ == "__main__":
    import DataBase.Tables.Person
    import DataBase.Tables.Address
    person_db.create_all()
    address_db.create_all()

在Eclipse中我仍然出现错误:

I still get an error in Eclipse:

* sqlalchemy.exc.NoReferencedTableError:与列"Address.person_id"相关联的外键找不到表"person",通过该表可生成目标列"sid"的外键*

*sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'Address.person_id' could not find table 'person' with which to generate a foreign key to target column 'sid'*

我可以找到另一条有人建议使用元数据"的帖子,但我找不到合适的使用方式.

I could find another post with someone suggesting the use of "metadata" but I couldn't find a proper way to use that.

有人有解决这个问题的想法吗?

Does anyone have an idea to solve this ?

谢谢!

推荐答案

您只需拥有以下一组,而不必为每个模型单独准备一份副本:

You need to have only one set of the below, and not a separate copy for each model:

app = Flask(my_app_name)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///C:\\MyBase\\Base.sqlite'
db = SQLAlchemy(app)

这可以在单独的模块中定义(将其称为shared),并导入到每个模型定义文件中.
在这种情况下,主模块将更像:

This can be defined in a separate module (lets call it shared), and imported into each model definition file.
In this case the main module will look more like:

from DataBase.Tables.shared import db

if __name__ == "__main__":
    import DataBase.Tables.Person   # will load Person model into the db
    import DataBase.Tables.Address  # will load Address model into the db
    db.create_all() # will create all models

这篇关于Flask Sqlalchemy:不同模块之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:42
查看更多