如果我定义一个表没有主键:为什么Flask-SQLAlchemy需要主键?
class CustomAttribute(db.Model):
player = db.Column(db.Integer, db.ForeignKey('player.id'))
key = db.Column(db.Text, nullable=False)
value = db.Column(db.Text, nullable=False)
我得到一个错误:
sqlalchemy.exc.InvalidRequestError: Class <class 'rpgquest.models.CustomAttribute'> does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class.
唯一的解决方法是手动定义__tablename__
,但是这是为什么需要?
我不需要主键,因为唯一的请求是让所有玩家获得一个X的键值对,或者获得某个玩家的所有键值对,并且玩家不能拥有重复的键。
你有一个很好的主键候选:'(播放器,键)'。 –
Ooooh ...你让我再次感到愚蠢:D @IljaEverilä –