【问题标题】:Relationships in SQLAlchemy are not working as expectedSQLAlchemy 中的关系没有按预期工作
【发布时间】:2018-11-17 09:37:36
【问题描述】:

我正在尝试在两个表之间创建关系;事件和地点。 我想要完成的事情是创建一对多的关系。一个事件可以在一个位置进行,因此该位置必须能够举办各种事件。我一直在摆弄外键几个小时,但我不知道如何让它工作。

我的 location.py 如下所示:

from datetime import datetime

from sqlalchemy import Column, Integer, ForeignKey, Float, Date, String
from sqlalchemy.orm import relationship

from model import Base

class Location(Base):
    __tablename__ = 'location'
    id = Column(Integer, primary_key=True, nullable=False)
    title = Column(String, unique=True)
    description = Column(String, nullable=False)
    founder_id = Column(Integer, ForeignKey('users.id'))
    latitude = Column(Float)
    longtitude = Column(Float)
    creation_date = Column(Date, default=datetime.now)


    # Relationships
    creator = relationship('User', foreign_keys=founder_id)
    events = relationship('Event', back_populates='location')

event.py 看起来像这样:

from datetime import datetime

from sqlalchemy import Column, Integer, Date, Float, String, ForeignKey
from sqlalchemy.orm import relationship

from model import Base
from model.event_contestant import EventContestant
from model.location import Location


class Event(Base):
    __tablename__ = 'event'
    id = Column(Integer, primary_key=True)
    title = Column(String, nullable=False, unique=True)
    date = Column(Date, nullable=False)
    location_id = Column(Integer),
    longtitude = Column(Float, nullable=False)
    latitude = Column(Float, nullable=False)
    description = Column(String, nullable=False)
    difficulty = Column(Integer, nullable=False, default=3)
    creation_date = Column(Date, nullable=False, default=datetime.now)
    event_creator = Column(Integer, ForeignKey('users.id'), nullable=False)
    event_type = Column(String, nullable=False, default='Freestyle')

    # Relationships
    creator = relationship("User")
    contestants = relationship(EventContestant, back_populates="event")
    location = relationship('Location', foreign_key=location_id)

我从 sqlalchemy 获得的 stacttrace 如下所示:

sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|Location|location'. Original exception was: Could not determine join condition between parent/child tables on relationship Location.events - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

我真的很困惑,不知道为什么这个一对多应该可以工作。

【问题讨论】:

    标签: python sqlalchemy


    【解决方案1】:

    Event 需要一个指向LocationForeignKey 来构建关系。具体来说,您想更改 Event.location_id 的定义以添加 ForeignKey('location.id')(并去掉结尾的逗号;Event.location_id 当前是一个元组)。

    【讨论】:

    • 我尝试将 location_id 设置为绑定在 Location.id 上的外键,但这并没有看到 mto 工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    相关资源
    最近更新 更多