Python SQLAlchemy关系问题-映射程序找不到名称
我有两张桌子,试图建立一对多的关系。应该有一个具有多个Python SQLAlchemy关系问题-映射程序找不到名称,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,我有两张桌子,试图建立一对多的关系。应该有一个具有多个会话的播放器 import sqlalchemy as sql from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class Session(Base): __tablename__ = 'session' id = sql.Co
会话的播放器
import sqlalchemy as sql
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Session(Base):
__tablename__ = 'session'
id = sql.Column('session_id', sql.INTEGER, primary_key=True)
player_id = sql.Column(sql.INTEGER, sql.ForeignKey('player.player_id'))
class Player(Base):
__tablename__ = 'player'
id = sql.Column('player_id', sql.INTEGER, primary_key=True)
sessions = relationship('Session', backref='player')
但是,我在创建数据库时不断遇到的错误是
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize -
can't proceed with initialization of other mappers. Triggering mapper:
'Mapper|Player|player'. Original exception was: When initializing mapper
Mapper|Player|player, expression 'Session' failed to locate a name ("name
'Session' is not defined"). If this is a class name, consider adding this
relationship() to the <class 'analysis_db_tables.Player'> class after both
dependent classes have been defined.
我还尝试在这两个类中使用back\u用relationship
语句填充。我做错了什么
这个问题似乎是在构建数据库代码时随机出现的,我在重新创建问题的同时将表与这两个表配对。我在postgres中使用SQLAlchemy。谢谢。试试:
sessions = sql.relationship('Session', backref='player')
而不是:
sessions = relationship('Session', backref='player')
进行此更改只会引发以下错误:sqlalchemy.exc.norreferencedcolumnerror:无法初始化表“session”上ForeignKey“player.id”的目标列:表“player”没有名为“id”的列
抱歉,我没有注意到您在列中包含了名称“product\u id”。我对另一个可能的问题修改了答案。谢谢,但也不是那样。我直接从sqlalchemy.orm导入关系中获取关系
sessions = relationship('Session', backref='player')