Skip to content

relaytionships example from https://sqlmodel.tiangolo.com/ does not work #334

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

Closed
8 tasks done
mythral0 opened this issue May 6, 2022 · 3 comments
Closed
8 tasks done
Labels
question Further information is requested

Comments

@mythral0
Copy link

mythral0 commented May 6, 2022

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

from typing import List, Optional

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: Optional[int] = Field(default=None, foreign_key="team.id")
    team: Optional[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)


def create_heroes():
    with Session(engine) as session:
        team_preventers = Team(name="Preventers", headquarters="Sharp Tower")
        team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")

        hero_deadpond = Hero(name="Deadpond", secret_name="Dive Wilson", team=team_z_force)
        hero_rusty_man = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48, team=team_preventers)
        hero_spider_boy = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
        session.add(hero_deadpond)
        session.add(hero_rusty_man)
        session.add(hero_spider_boy)
        session.commit()

        session.refresh(hero_deadpond)
        session.refresh(hero_rusty_man)
        session.refresh(hero_spider_boy)

        print("Created hero:", hero_deadpond)
        print("Created hero:", hero_rusty_man)
        print("Created hero:", hero_spider_boy)


def main():
    create_db_and_tables()
    create_heroes()


if __name__ == "__main__":
    main()

Description

teams and heros relaytionship is not working.

Operating System

Linux

Operating System Details

Debian11 all current updates have been applied

SQLModel Version

0.0.6

Python Version

3.9.2

Additional Context

(venv) root@Dev1:/home/SimpleHero# python test1.py
2022-05-06 10:17:44,869 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2022-05-06 10:17:44,869 INFO sqlalchemy.engine.Engine PRAGMA main.table_info("team")
2022-05-06 10:17:44,869 INFO sqlalchemy.engine.Engine [raw sql] ()
2022-05-06 10:17:44,870 INFO sqlalchemy.engine.Engine PRAGMA temp.table_info("team")
2022-05-06 10:17:44,870 INFO sqlalchemy.engine.Engine [raw sql] ()
2022-05-06 10:17:44,870 INFO sqlalchemy.engine.Engine PRAGMA main.table_info("hero")
2022-05-06 10:17:44,870 INFO sqlalchemy.engine.Engine [raw sql] ()
2022-05-06 10:17:44,871 INFO sqlalchemy.engine.Engine PRAGMA temp.table_info("hero")
2022-05-06 10:17:44,871 INFO sqlalchemy.engine.Engine [raw sql] ()
2022-05-06 10:17:44,871 INFO sqlalchemy.engine.Engine
CREATE TABLE team (
id INTEGER,
name VARCHAR NOT NULL,
headquarters VARCHAR NOT NULL,
PRIMARY KEY (id)
)

2022-05-06 10:17:44,871 INFO sqlalchemy.engine.Engine [no key 0.00010s] ()
2022-05-06 10:17:44,893 INFO sqlalchemy.engine.Engine CREATE INDEX ix_team_name ON team (name)
2022-05-06 10:17:44,893 INFO sqlalchemy.engine.Engine [no key 0.00017s] ()
2022-05-06 10:17:44,906 INFO sqlalchemy.engine.Engine
CREATE TABLE hero (
id INTEGER,
name VARCHAR NOT NULL,
secret_name VARCHAR NOT NULL,
age INTEGER,
team_id INTEGER,
PRIMARY KEY (id),
FOREIGN KEY(team_id) REFERENCES team (id)
)

2022-05-06 10:17:44,906 INFO sqlalchemy.engine.Engine [no key 0.00021s] ()
2022-05-06 10:17:44,920 INFO sqlalchemy.engine.Engine CREATE INDEX ix_hero_age ON hero (age)
2022-05-06 10:17:44,920 INFO sqlalchemy.engine.Engine [no key 0.00023s] ()
2022-05-06 10:17:44,933 INFO sqlalchemy.engine.Engine CREATE INDEX ix_hero_name ON hero (name)
2022-05-06 10:17:44,933 INFO sqlalchemy.engine.Engine [no key 0.00021s] ()
2022-05-06 10:17:44,946 INFO sqlalchemy.engine.Engine COMMIT
2022-05-06 10:17:44,950 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2022-05-06 10:17:44,953 INFO sqlalchemy.engine.Engine INSERT INTO hero (name, secret_name, age, team_id) VALUES (?, ?, ?, ?)
2022-05-06 10:17:44,953 INFO sqlalchemy.engine.Engine [generated in 0.00039s] ('Deadpond', 'Dive Wilson', None, None)
2022-05-06 10:17:44,954 INFO sqlalchemy.engine.Engine INSERT INTO hero (name, secret_name, age, team_id) VALUES (?, ?, ?, ?)
2022-05-06 10:17:44,955 INFO sqlalchemy.engine.Engine [cached since 0.001634s ago] ('Rusty-Man', 'Tommy Sharp', 48, None)
2022-05-06 10:17:44,955 INFO sqlalchemy.engine.Engine INSERT INTO hero (name, secret_name, age, team_id) VALUES (?, ?, ?, ?)
2022-05-06 10:17:44,955 INFO sqlalchemy.engine.Engine [cached since 0.00213s ago] ('Spider-Boy', 'Pedro Parqueador', None, None)
2022-05-06 10:17:44,956 INFO sqlalchemy.engine.Engine COMMIT
2022-05-06 10:17:44,967 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2022-05-06 10:17:44,969 INFO sqlalchemy.engine.Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id
FROM hero
WHERE hero.id = ?
2022-05-06 10:17:44,969 INFO sqlalchemy.engine.Engine [generated in 0.00017s] (1,)
2022-05-06 10:17:44,970 INFO sqlalchemy.engine.Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id
FROM hero
WHERE hero.id = ?
2022-05-06 10:17:44,970 INFO sqlalchemy.engine.Engine [cached since 0.001173s ago] (2,)
2022-05-06 10:17:44,970 INFO sqlalchemy.engine.Engine SELECT hero.id, hero.name, hero.secret_name, hero.age, hero.team_id
FROM hero
WHERE hero.id = ?
2022-05-06 10:17:44,970 INFO sqlalchemy.engine.Engine [cached since 0.001778s ago] (3,)
Created hero: age=None secret_name='Dive Wilson' name='Deadpond' team_id=None id=1
Created hero: age=48 secret_name='Tommy Sharp' name='Rusty-Man' team_id=None id=2
Created hero: age=None secret_name='Pedro Parqueador' name='Spider-Boy' team_id=None id=3
2022-05-06 10:17:44,971 INFO sqlalchemy.engine.Engine ROLLBACK
(venv) root@Dev1:/home/SimpleHero#

@mythral0 mythral0 added the question Further information is requested label May 6, 2022
@byrman
Copy link
Contributor

byrman commented May 6, 2022

I guess you bumped into #315.

@mythral0
Copy link
Author

mythral0 commented May 6, 2022

fantasic

I guess you bumped into #315.

Fantastic, yes, yes, yes that did the trick. Wow man, spent so long on this little rat thanks so much.

@mythral0 mythral0 closed this as completed May 6, 2022
@mythral0
Copy link
Author

mythral0 commented May 6, 2022

Problem solved. Please follow the link for the resolution.

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

2 participants