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

📝 Update instructions about how to make a foreign key required in docs/tutorial/relationship-attributes/define-relationships-attributes.md #474

Merged

Conversation

jalvaradosegura
Copy link
Contributor

@jalvaradosegura jalvaradosegura commented Oct 21, 2022

Hi 👋

I wanted to make a foreign key mandatory and I think that this part of the tutorial was talking about that. I followed those steps but the foreign key wasn't mandatory yet. It didn't show the red "*" on the Swagger interface and my unit tests were passing without it.

I completely removed the the default parameter of the Field object, of the foreign key, and after that, I got the red "*" and my unit tests started to fail.

I'm not sure if this is how it is supposed to be done, but in case it is, here is the PR :)

Just in case, here I'm sharing a little code with an app to test this. First run it as it is (the current state of the code is after applying the instructions of the docs before this PR) and then go to the /docs and check the schema for the heroes endpoint. team_id shouldn't be mandatory. But if you remove the default parameter of the Field object of the team_id attribute, it should become mandatory

from typing import List, Optional

from fastapi import FastAPI
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine


class Team(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str = Field(index=True)
    headquarters: str

    heroes: List["Hero"] = Relationship(back_populates="team")


class Hero(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str = Field(index=True)
    secret_name: str
    age: Optional[int] = Field(default=None, index=True)

    team_id: int = Field(default=None, foreign_key="team.id")
    team: Team = Relationship(back_populates="heroes")


sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"

engine = create_engine(sqlite_url, echo=True)


def create_db_and_tables():
    SQLModel.metadata.create_all(engine)


app = FastAPI()


@app.on_event("startup")
def on_startup():
    create_db_and_tables()


@app.post("/heroes/")
def create_hero(hero: Hero):
    with Session(engine) as session:
        session.add(hero)
        session.commit()
        session.refresh(hero)
        return hero

ps: I'm having tons of fun with your libraries! thanks

@github-actions
Copy link

📝 Docs preview for commit b3386f3 at: https://635230ff381fa0723e828238--sqlmodel.netlify.app

@codecov
Copy link

codecov bot commented Oct 21, 2022

Codecov Report

Base: 98.49% // Head: 98.49% // No change to project coverage 👍

Coverage data is based on head (b3386f3) compared to base (ea79c47).
Patch has no changes to coverable lines.

❗ Current head b3386f3 differs from pull request most recent head 8b6a4d6. Consider uploading reports for the commit 8b6a4d6 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #474   +/-   ##
=======================================
  Coverage   98.49%   98.49%           
=======================================
  Files         185      185           
  Lines        5856     5856           
=======================================
  Hits         5768     5768           
  Misses         88       88           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@github-actions
Copy link

📝 Docs preview for commit 8b6a4d6 at: https://639ce0735cc7fa00ac620965--sqlmodel.netlify.app

@tiangolo tiangolo changed the title Add a missing piece to make a foreign key required 📝 Update instructions about how to make a foreign key required in docs/tutorial/relationship-attributes/define-relationships-attributes.md Oct 22, 2023
@tiangolo
Copy link
Member

Great, thanks! 🍰

@tiangolo tiangolo merged commit bdcf11b into fastapi:main Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants