Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to accomplish Read/Write transactions with a one to many relationship #176

Open
8 tasks done
br-follow opened this issue Dec 3, 2021 · 0 comments
Open
8 tasks done
Labels
question Further information is requested

Comments

@br-follow
Copy link
Contributor

br-follow commented Dec 3, 2021

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

class User(SQLModel):
  __tablename__ = "users"
  id: Optional[str]
  cars: List[Car] = Relationship(sa_relationship=RelationshipProperty("Car", back_populates="user")

class Car(SQLModel):
  ...
  user_id: str = Field(default=None, foreign_key="users.id")
  user: User = Relationship(sa_relationship=RelationshipProperty("User", back_populates="cars"))
  is_main_car: bool

Description

I have two tables that have a many to one relationship, such as the one described above. Any given user can only have a single car that is_main_car. Additionally, the first car a user gets must be the main car.

I am trying to determine how the transactional semantics work with this relationship within a Session. If I read the user, and then use the user.cars field to determine if the user has 0 cars or already has a main car, can I rely on that condition still being true when I write my new main Car row to the Cars table (assuming it is all within a single Session)?

Operating System

macOS

Operating System Details

No response

SQLModel Version

0.0.4

Python Version

3.9.7

Additional Context

No response

@br-follow br-follow added the question Further information is requested label Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant