1

当我尝试向 SQLAlchemy 添加新模型时出现错误(我在下面使用 MySQL)

raise errorclass, errorvalue
sqlalchemy.exc.OperationalError 1005 Can't create table

我从一开始就有模型/表 PlayerModel/players,我现在正在尝试添加新表(Base 有 id 列 id)

class Base(object):
    def __tablename__(self):
        return self.__name__.lower()

    id = Column(Integer, primary_key=True, nullable=False)
class PlayerModel(Base):
    __tablename__ = 'players'
    alliances_id = Column(Integer, ForeignKey('alliances.id'), nullable=True)
    alliance = relationship('AllianceModel')
    username = Column(String(90), nullable=True, default=None)
    email = Column(String(75), nullable=True, default=None)
    password = Column(String(128), nullable=True, default=None)
    money = Column(Integer, nullable=False, default=INITIAL_MONEY)
    # used for push notifications
    push_token = Column(String(64), nullable=True)
    registered_for_push = Column(Boolean, nullable=False, default=False)
    device_id = Column(String(64), nullable=True)
    device_type = Column(String(64), nullable=True)
    device_os = Column(Integer, nullable=True)
    notify = Column(Boolean, default=True)


    def __repr__(self):
        return "<Player('%s')>" % (self.type)

并在这里抛出错误

class ArmyModel(Base):
    __tablename__ = 'army'

    player_id = Column(Integer, ForeignKey('players.id', ondelete='CASCADE'), nullable=False, index=True)
    speed = Column(Float, nullable=False, default=1)
    # mode of army behavior ( always fight, ignore )
    mode = Column(Integer, nullable=False)
    x = Column(Float, nullable=False, default=0)
    y = Column(Float, nullable=False, default=0)
    z = Column(Float, nullable=False, default=0)

    def __repr__(self):
        return "<Army('%s')>" % (self.type)
4

0 回答 0