NameError:名称“ ForeignKeyConstraint”未定义

时间:2020-04-06 14:50:50

标签: python sqlalchemy flask-sqlalchemy

我正在尝试使用SQLAlchemy创建复合约束,但我不知道发生了什么事情:

class City(db.Model):
__tablename__ = 'cities'
__table_args__ = (db.UniqueConstraint('city', 'state', name="unique_city"),)

city = db.Column(db.String(120), primary_key=True)
state = db.Column(db.String(120), primary_key=True)
venue = db.relationship('Venue', backref='city', lazy=True)

class Venue(db.Model):
__tablename__ = 'venue'

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
address = db.Column(db.String(120))
city = db.Column(db.String(120))
state = db.Column(db.String(120))
phone = db.Column(db.String(120))
image_link = db.Column(db.String(500))
facebook_link = db.Column(db.String(120))
venue_genre = db.relationship('VenueGenre', backref='venue', lazy=True)
show = db.relationship('Show', backref='venue', lazy=True)

__table_args__ = (ForeignKeyConstraint(
    [city, state],
    [city.city, city.state]),
)

我收到此错误消息:

NameError: name 'ForeignKeyConstraint' is not defined

这在python中已正确缩进,不确定在此如何执行。我觉得这很容易解决,但我对此并不陌生,似乎无法解决。

0 个答案:

没有答案