我正在 Flask 中构建一个简单的应用程序,并且在使用 Flask-SQLAlchemy 时难以使数据库正常运行。这是我的models.py:
from spot import plate
from spot import db
import flask.ext.whooshalchemy as whooshalchemy
class Country(db.Model):
id = db.Column(db.Integer, primary_key = True)
country_name = db.Column(db.String(64), unique = True)
codes = db.relationship('Code', backref='country', lazy='dynamic')
def __repr__(self):
return '<%r>' % (self.country_name)
class Code(db.Model):
__searchable__ = ['scramble']
id = db.Column(db.Integer, primary_key = True)
scramble = db.Column(db.String(64), db.ForeignKey('country.id'))
def __repr__(self):
return '<%r>' % (self.scramble)
whooshalchemy.whoosh_index(plate, Code)
我正在尝试创建一些测试数据以查看关系是否按预期工作,但由于某种原因,当我创建一个新代码并将其提交到数据库时,有些东西将我的 scramble 字段的值覆盖为“ 1”,如下:
>>> from spot.models import Country, Code
>>> from spot import db
>>> c = Country(country_name="Testingstan")
>>> cd = Code(scramble="TS", country=c)
>>> cd.scramble
'TS'
>>> db.session.add(c)
>>> c.country_name
'Testingstan'
>>> db.session.add(cd)
>>> cd.scramble
'TS'
>>> db.session.commit()
>>> c.country_name
u'Testingstan'
>>> cd.scramble
u'1'
>>> for code in c.codes:
... print code
...
<u'1'>
所以我确信国家和代码之间的关联是正确的,但是为什么 SQLAlchemy 覆盖了我的打乱而不是我的国家名称?我在 sqlite 数据库浏览器中打开了数据库,它向我展示了同样的东西。我有什么设置不正确?