带有SQLAlchemy表的Flask没有链接

时间:2015-05-31 12:01:28

标签: python flask sqlalchemy flask-sqlalchemy

我一直在努力解决这个小小的SQLAlchemy问题。它告诉我,City.address引用列并不与ForeignKeyConstraint相关联。

这是我的模特

#Address model
class Address(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    address = db.Column(db.String(100), nullable=False)

    company_id = db.Column(db.Integer, db.ForeignKey('company.id'))
    city_id = db.Column(db.Integer, db.ForeignKey('city.id'), nullable=False)
    event = db.relationship('Address', backref='event', lazy='dynamic')
    ForeignKeyConstraint(['city_id'],['city.city_id'])

    def __init__(self, address):
        self.address = address

    def __repr__(self):
        return '<Address %r>' % self.address

#City model
class City(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    zip_code = db.Column(db.String(10), unique=True)
    city_name = db.Column(db.String(100), unique=True)
    country = db.Column(db.String(100), unique=True)

    address = db.relationship('City', backref='address', lazy='dynamic')
    events = db.relationship('City', backref='event', lazy='dynamic')

    PrimaryKeyConstraint('id', 'city_id', name='city_pk')


    def __init__(self, zipcode, cityname, country):
        self.zip_code = zipcode
        self.city_name = cityname
        self.country = country

    def __repr__(self):
        return '<City %r>' % self.cityName

我确定这只是一个小问题,但希望另外一套眼睛可以帮助。

预先感谢

2 个答案:

答案 0 :(得分:0)

查看SQLAlchemy Documentation : Building a Relationship

我没有尝试使用FLASK,但它应该可以使用relationship

答案 1 :(得分:0)

#City model
class City(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    zip_code = db.Column(db.String(10), unique=True)
    city_name = db.Column(db.String(100), unique=True)
    country = db.Column(db.String(100), unique=True)

    address = db.relationship('City', backref='address', lazy='dynamic')
    events = db.relationship('City', backref='event', lazy='dynamic')

&#39;市&#39;和&#39;地址&#39;不得不换位置。