the max_length in string field is not working! #904
-
First Check
Commit to Help
Example Codelet's give a more detail example;
app.py
from sqlmodel import Session
from database import create_db_and_tables, engine
from models import Hero, Team
def create_heroes():
with Session(engine) as session:
team_z_force = Team(name="Z-Forcexixixisfds", headquarters="Sister Margaret's Bar")
hero_deadpond = Hero(
name="Deadpondasdfads", secret_name="Dive Wilson", team=team_z_force, age=50
)
session.add(hero_deadpond)
session.commit()
session.refresh(hero_deadpond)
print("Created hero:", hero_deadpond)
print("Hero's team:", hero_deadpond.team)
def main():
create_db_and_tables()
create_heroes()
if __name__ == "__main__":
main()
models.py:::::
from typing import List, Optional
from sqlmodel import Field, Relationship, SQLModel
class Team(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(index=True, max_length=5)
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, max_length=5, min_length=4)
secret_name: str
age: Optional[int] = Field(default=None, index=True, ge=0, le=1)
team_id: Optional[int] = Field(default=None, foreign_key="team.id")
team: Optional[Team] = Relationship(back_populates="heroes") Descriptionmy question::: please help Operating SystemmacOS Operating System DetailsNo response SQLModel Version0.0.16 Python Version3.10.9 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
joachimhuet
Apr 26, 2024
Replies: 1 comment 4 replies
-
This is embarrassing indeed, what you could do is use a pure SQLModel model object and inherit it: class BaseTeam(SQLModel):
name: str = Field(index=True, max_length=5)
headquarters: str
class Team(BaseTeam, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
heroes: List["Hero"] = Relationship(back_populates="team")
class BaseHero(SQLModel):
name: str = Field(index=True, max_length=5, min_length=4)
secret_name: str
age: Optional[int] = Field(default=None, index=True, ge=0, le=1)
team: Optional[Team] = None
class Hero(BaseHero, table=True):
id: Optional[int] = Field(default=None, primary_key=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)
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
team_z_force = TeamBase(name="Z-Forcexixixisfds", headquarters="Sister Margaret's Bar")
team = Team.model_validate(team_z_force)
session.add(team)
session.commit()
session.refresh(team)
hero_deadpond = HeroBase(
name="Deadpondasdfads", secret_name="Dive Wilson", team=team, age=50
)
hero = Hero(
**hero_deadpond.model_dump(exclude={"team"}),
team_id=hero_deadpond.team.id if hero_deadpond.team else None
)
session.add(hero)
session.commit()
session.refresh(hero)
print("Created hero:", hero)
print("Hero's team:", hero.team) |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ouch, this just means you have to reset and start again, a table can only be defined once per instance.
if you are working in a notebook for example and execute the code that defines
Team
you have to restart the notebook before re-executing that cell or you will get that error.And you must be careful not to declare a table that share the same name 2 times in your code execution.
Example:
First execution:
Second execution: