不使用主键创建具有一对多关系的表
Creating tables with a one to many relationship not using primary key
所以我试图在这两个 table 之间实现一个简单的一对多关系。我已经阅读了文档,搜刮了网络,投入了大量工作来解决这个问题,所以我求助于你。
我拥有对数据库的完全访问权限,并且能够创建其他 tables,具有类似的工作关系。
我正在使用 mariadb,"mysql"。
tableTradable 中的每一行都有一个 tick_size_id,每一行都有一个 tick_size_id。我想将它们与该专栏联系起来,我似乎无法弄清楚如何。
base = declarative_base()
class Tradables(base):
__tablename__ = "tradables"
id = Column(Integer, primary_key=True)
tick_size_id = Column(Integer, nullable=False)
ticks = relationship("Ticks")
class Ticks(base):
__tablename__ = "ticks"
id = Column(Integer, primary_key=True)
tick_size_id = Column(Integer, ForeignKey("tradables.tick_size_id"))
def main():
engine = create_engine("mysql+pymysql://user:password@localhost/database?host=localhost?port=3306")
base.metadata.create_all(engine)
if __name__ == '__main__':
main()
这不起作用。
并失败:
sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1005, '可以\'t create table Trading
.ticks
(errno: 150 "Foreign key constraint is incorrectly formed")') [SQL: '\n
CREATE TABLE ticks (\n\t
id INTEGER NOT NULL AUTO_INCREMENT, \n\t
tick_size_id INTEGER, \n\t
PRIMARY KEY (id), \n\t
FOREIGN KEY(tick_size_id) REFERENCES tradables (tick_size_id)\n)\n\n']
我做错了什么?
编辑:
尝试了这两种创建顺序,结果相同。
base.metadata.tables["ticks"].create(bind=engine)
base.metadata.tables["tradables"].create(bind=engine)
和
base.metadata.tables["tradables"].create(bind=engine)
base.metadata.tables["ticks"].create(bind=engine)
您需要在 tradables.tick_size_id
上建立索引。我不是炼金术士,但我想应该是这样的
...
__tablename__ = "tradables"
id = Column(Integer, primary_key=True)
tick_size_id = Column(Integer, nullable=False, index=True)
ticks = relationship("Ticks")
...
所以我试图在这两个 table 之间实现一个简单的一对多关系。我已经阅读了文档,搜刮了网络,投入了大量工作来解决这个问题,所以我求助于你。
我拥有对数据库的完全访问权限,并且能够创建其他 tables,具有类似的工作关系。
我正在使用 mariadb,"mysql"。
tableTradable 中的每一行都有一个 tick_size_id,每一行都有一个 tick_size_id。我想将它们与该专栏联系起来,我似乎无法弄清楚如何。
base = declarative_base()
class Tradables(base):
__tablename__ = "tradables"
id = Column(Integer, primary_key=True)
tick_size_id = Column(Integer, nullable=False)
ticks = relationship("Ticks")
class Ticks(base):
__tablename__ = "ticks"
id = Column(Integer, primary_key=True)
tick_size_id = Column(Integer, ForeignKey("tradables.tick_size_id"))
def main():
engine = create_engine("mysql+pymysql://user:password@localhost/database?host=localhost?port=3306")
base.metadata.create_all(engine)
if __name__ == '__main__':
main()
这不起作用。
并失败:
sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1005, '可以\'t create table Trading
.ticks
(errno: 150 "Foreign key constraint is incorrectly formed")') [SQL: '\n
CREATE TABLE ticks (\n\t
id INTEGER NOT NULL AUTO_INCREMENT, \n\t
tick_size_id INTEGER, \n\t
PRIMARY KEY (id), \n\t
FOREIGN KEY(tick_size_id) REFERENCES tradables (tick_size_id)\n)\n\n']
我做错了什么?
编辑:
尝试了这两种创建顺序,结果相同。
base.metadata.tables["ticks"].create(bind=engine)
base.metadata.tables["tradables"].create(bind=engine)
和
base.metadata.tables["tradables"].create(bind=engine)
base.metadata.tables["ticks"].create(bind=engine)
您需要在 tradables.tick_size_id
上建立索引。我不是炼金术士,但我想应该是这样的
...
__tablename__ = "tradables"
id = Column(Integer, primary_key=True)
tick_size_id = Column(Integer, nullable=False, index=True)
ticks = relationship("Ticks")
...