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

时间:2017-07-13 06:54:15

标签: python sqlalchemy

我遇到了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.

所以我收集问题必须是一个表'匹配',它是我之前创建的外键。我如何摆脱'匹配'表的这些残余,以便我可以构建这些其他表?

0 个答案:

没有答案