SqlAlchemy Core: define one-to-one relationship multiple time

225 views Asked by At

I have now implemented an events table with relationships to three other tables through One-to-Many .

events = Table(
    "events",
    metadata,
    Column("id", Integer(), primary_key=True),
    Column("creator", ForeignKey(users.c.id, ondelete="SET NULL")),
    ...
    Column("location_id", ForeignKey(locations.c.id, ondelete="SET NULL"), nullable=False),
    Column("activities_id", ForeignKey(activities.c.id, ondelete="SET NULL"), nullable=False),
    UniqueConstraint("location_id", "id", name="event_location")
)

users = Table(
    "users",
    metadata,
    Column("id", Integer, primary_key=True),
    ......

activities = Table(
    "activities",
    metadata,
    Column("id", Integer(), primary_key=True),
    ....

locations = Table(
    "locations",
    metadata,
    Column("id", Integer(), primary_key=True),
    ....

But such a connection does not satisfy the business logic. An event object can only have one creator, location and activity.

Now I want to define one-to-one relationships for all ForeignKey fields. I found solution with define ForeignKey as primary_key=True (How to define one-to-one and many-to-many relationship using SQLAlchemy Core) but how to do this with three fields I don't understand.

0

There are 0 answers