Python SqlAlchemy-没有外键链接这些表。过去创建的表

Python SqlAlchemy-没有外键链接这些表。过去创建的表,python,sqlalchemy,Python,Sqlalchemy,我对sqlalchemy有一个问题,它阻止了我取得任何进展。我已经创建了其他具有关系的表,但是这个表似乎不起作用,因为我以前创建了一些错误的表。下面的代码是测试,当我运行它时,我得到了我粘贴在代码下面的错误: import os import sys from sqlalchemy import Column, ForeignKey, Integer, String, Boolean from sqlalchemy.ext.declarative import declarative_base

我对sqlalchemy有一个问题,它阻止了我取得任何进展。我已经创建了其他具有关系的表,但是这个表似乎不起作用,因为我以前创建了一些错误的表。下面的代码是测试,当我运行它时,我得到了我粘贴在代码下面的错误:

import os 
import sys
from sqlalchemy import Column, ForeignKey, Integer, String, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine

Base = declarative_base()

class X(Base):
    __tablename__ = 'x2'
    pid = Column(Integer, primary_key=True)
    color = Column(String(4), nullable=False)
    player = relationship('Y')

class Y(Base):
    __tablename__ = 'y2'
    name = Column(String(16), nullable=False)
    id = Column(Integer, primary_key=True)
    champion = Column(String(16), nullable=False)
    kills = Column(Integer, nullable=False)
    deaths = Column(Integer, nullable=False)
    team_id = Column(Integer, ForeignKey('x2.pid'))



engine = create_engine('postgresql://db')
Base.metadata.create_all(engine)



from sqlalchemy import create_engine 
from sqlalchemy.orm import sessionmaker
#from lol_db_setup import Base, Match, Team, Player

engine = create_engine('postgresql://heyfinn:lolpassword@leagueoflegendsdb.c3jpkci5dhiy.ap-southeast-2.rds.amazonaws.com:5432/leagueoflegendsdb')
Base.metadata.bind = engine
DBSession = sessionmaker(bind = engine)
session = DBSession()
teamEntry = X(pid=1234, color = 'red')
newEntry = Y(name='heyfinn', id=622740, champion = 'Zac', kills=10, deaths = 0, team_id= 1234)
session.commit()


---------------------------------------------------------------------------
InvalidRequestError                       Traceback (most recent call last)
<ipython-input-33-56c613d76617> in <module>()
      8 DBSession = sessionmaker(bind = engine)
      9 session = DBSession()
---> 10 teamEntry = X(id=1234, color = 'red')
     11 newEntry = Y(name='heyfinn', id=622740, champion = 'Zac', kills=10, deaths = 0, team_id= 1234)
     12 session.commit()

<string> in __init__(self, **kwargs)
InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 
'Mapper|Match|match'. Original exception was: Could not determine join condition between parent/child tables on relationship Match.team - there are no foreign keys linking these tables.  
Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
导入操作系统
导入系统
从sqlalchemy导入列,ForeignKey,Integer,String,Boolean
从sqlalchemy.ext.declarative导入声明性基础
从sqlalchemy.orm导入关系
从sqlalchemy导入创建引擎
Base=声明性_Base()
X类(基本):
__tablename_uux2'
pid=列(整数,主键=真)
颜色=列(字符串(4),可空=假)
player=关系('Y')
Y类(基本):
__tablename_uuu='y2'
name=Column(字符串(16),null=False)
id=列(整数,主键=True)
champion=Column(字符串(16),null=False)
kills=Column(整数,null=False)
死亡=列(整数,可空=False)
team_id=列(整数,ForeignKey('x2.pid'))
引擎=创建引擎('postgresql://db')
Base.metadata.create_all(引擎)
从sqlalchemy导入创建引擎
从sqlalchemy.orm导入sessionmaker
#从lol_db_设置导入基地、比赛、团队、球员
引擎=创建引擎('postgresql://heyfinn:lolpassword@leagueoflegendsdb.c3jpkci5dhiy.ap-southest-2.rds.amazonaws.com:5432/leagueoflegendsdb')
Base.metadata.bind=引擎
DBSession=sessionmaker(bind=engine)
session=DBSession()
teamEntry=X(pid=1234,颜色为“红色”)
newEntry=Y(name='heyfinn',id=622740,champion='Zac',kills=10,deatures=0,team_id=1234)
session.commit()
---------------------------------------------------------------------------
InvalidRequestError回溯(最近一次呼叫上次)
在()
8 DBSession=sessionmaker(bind=engine)
9 session=DBSession()
--->10 teamEntry=X(id=1234,颜色为“红色”)
11 newEntry=Y(name='heyfinn',id=622740,champion='Zac',kills=10,deadcess=0,team_id=1234)
12会话提交()
在uuuu init uuuuuuuuuuuuu中(self,**kwargs)
InvalidRequestError:一个或多个映射程序未能初始化-无法继续初始化其他映射程序。触发映射器:
“Mapper | Match | Match”。原始异常是:无法确定关系Match.team上的父/子表之间的联接条件-没有链接这些表的外键。
确保引用列与ForeignKey或ForeignKeyConstraint关联,或指定“primaryjoin”表达式。

因此,我认为问题一定出在一个表“match”上,它是我之前创建的外键。如何去除“匹配”表的剩余部分,以便只构建这些其他表?

为了更新,我创建了上述的另一个实例,但我仍然不明白为什么会发生此错误。我认为这只是缺乏关于“映射器”的知识。当您声明一个类从
Base
派生时,SQLAlchemy将跟踪它,以便稍后可以初始化它。您需要创建一个新的
Base
,以使SQLAlchemy“忘记”以前定义的类。由于您是在IPython控制台中运行的,我假设您刚刚在与
Match
相同的
Base
上声明了
X
Y
。啊哈!哎呀,谢谢。我想我应该在看文件的时候注意一下。