我试图弄清楚如何在SQLAlchemy和Flask中使用关系。我的脚本不起作用,我真的不知道为什么......
我有两个类,Building和BuildingTypes。我们的想法是,BuildingTypes处理表中解释不同类型建筑物的位置,以及构建实际建筑物。建筑物中的一种方法需要特定类型建筑物的尺寸,因此会要求它,但我不能让它起作用......
该关系应该将多个Building实例绑定到一个BuildingTypes实例。
db_header.py
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
BuildingTypes.py
from db_header import db
##
#
class BuildingTypes(db.Model):
__tablename__ = 'buildingTypes'
id = db.Column(db.Integer, primary_key=True)
width = db.Column(db.Integer)
height = db.Column(db.Integer)
Building.py
from db_header import db
##
# Represent a Building
#
class Building(db.Model):
__tablename__ = 'buildings'
id = db.Column(db.Integer, primary_key=True)
x = db.Column(db.Integer)
y = db.Column(db.Integer)
typeID = db.Column(db.Integer, db.ForeignKey('buildingTypes.id'))
buildingTypes = db.relationship("BuildingTypes")
def __init__(self, x, y, type):
x = int(x)
y = int(y)
width = buildingTypes.width
height = buildingTypes.height
self.xleft = x-(width/2)
self.ytop = y-(height/2)
self.xright = x+(width/2)
self.ybottom = y+(height/2)
self.type = type