Refactor relationships in Event and User models
Clarify and fix relationship definitions by specifying foreign keys explicitly in User model. Add new relationships for gift categories and email templates to the Event model, and comment out unused EventUpdate relationship. These changes ensure better structure and future maintainability.
This commit is contained in:
@@ -43,15 +43,17 @@ class Event(Base, UUIDMixin, TimestampMixin):
|
||||
contact_email = Column(String)
|
||||
contact_phone = Column(String)
|
||||
|
||||
# Relationships remain the same...
|
||||
# Relationships
|
||||
creator = relationship("User", back_populates="created_events", foreign_keys=[created_by])
|
||||
theme = relationship("EventTheme", back_populates="events")
|
||||
managers = relationship("EventManager", back_populates="event")
|
||||
guests = relationship("Guest", back_populates="event")
|
||||
gift_items = relationship("GiftItem", back_populates="event")
|
||||
gifts = relationship("GiftItem", back_populates="event")
|
||||
gift_categories = relationship("GiftCategory", back_populates="event")
|
||||
media = relationship("EventMedia", back_populates="event")
|
||||
updates = relationship("EventUpdate", back_populates="event")
|
||||
# updates = relationship("EventUpdate", back_populates="event") # Keep commented out
|
||||
rsvps = relationship("RSVP", back_populates="event")
|
||||
email_templates = relationship("EmailTemplate", back_populates="event") # Add this line
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint('slug', name='uq_event_slug'),
|
||||
|
||||
@@ -15,10 +15,16 @@ class User(Base, UUIDMixin, TimestampMixin):
|
||||
is_superuser = Column(Boolean, default=False, nullable=False)
|
||||
preferences = Column(JSON)
|
||||
|
||||
# Add relationships
|
||||
# Fix relationships with explicit foreign_keys
|
||||
created_events = relationship("Event", back_populates="creator", foreign_keys="Event.created_by")
|
||||
managed_events = relationship("EventManager", back_populates="user")
|
||||
guest_profiles = relationship("Guest", back_populates="user")
|
||||
|
||||
# Specify which foreign key to use in EventManager (this is the key fix)
|
||||
managed_events = relationship(
|
||||
"EventManager",
|
||||
back_populates="user",
|
||||
foreign_keys="EventManager.user_id"
|
||||
)
|
||||
guest_profiles = relationship("Guest", back_populates="user", foreign_keys="Guest.user_id")
|
||||
|
||||
def __repr__(self):
|
||||
return f"<User {self.email}>"
|
||||
Reference in New Issue
Block a user