Я пытаюсь создать связь между двумя таблицами; событие и место.
То, что я пытаюсь сделать, это создать отношения один ко многим. Событие может быть дано в определенном месте, поэтому оно должно быть в состоянии содержать различные события. Я часами возился с внешними ключами и не могу понять, как заставить его работать.
Мой 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)
Stactrace, который я получаю из sqlalchemy, выглядит следующим образом:
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.
Я действительно озадачен и не могу понять, почему этот для многих должен просто работать.