You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
department=awaitDepartment(name="Science").save()
course=Course(name="Math", completed=False) # note - not savedawaitdepartment.courses.add(course)
assertcourse.pkisnotNone# child model was saved# relation on child model is set and FK column saved in dbassertcourses.department==department# relation on parent model is also setassertdepartment.courses[0] ==course
To Reproduce
fromtypingimportOptionalimportdatabasesimportsqlalchemyimportasyncioimportormarDATABASE_URL="sqlite:///db.sqlite"database=databases.Database(DATABASE_URL)
metadata=sqlalchemy.MetaData()
# get your database url in sqlalchemy format - same as used with databases instance used in Model definitionengine=sqlalchemy.create_engine(DATABASE_URL)
# note that this has to be the same metadata that is used in ormar Models definitionclassDepartment(ormar.Model):
classMeta:
database=databasemetadata=metadataid: int=ormar.Integer(primary_key=True)
name: str=ormar.String(max_length=100)
classCourse(ormar.Model):
classMeta:
database=databasemetadata=metadataid: int=ormar.Integer(primary_key=True)
name: str=ormar.String(max_length=100)
completed: bool=ormar.Boolean(default=False)
department: Optional[Department] =ormar.ForeignKey(Department)
asyncdefrun():
awaitdatabase.connect()
department=awaitDepartment(name="Science").save()
course=Course(name="Math", completed=False) # note - not savedawaitdepartment.courses.add(course)
awaitdatabase.disconnect()
if__name__=="__main__":
metadata.create_all(engine)
asyncio.run(run())
Error: ormar.exceptions.ModelPersistenceError: You cannot update not saved model! Use save or upsert method.
(Note: this should be a complete and concise piece of code that allows reproduction of an issue)
Expected behavior
A clear and concise description of what you expected to happen.
Screenshots
If applicable, add screenshots to help explain your problem.
Versions (please complete the following information):
Database backend used (mysql/sqlite/postgress)
Python 3.9.5
ormar 0.10.9
pydantic version
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered:
Describe the bug
https://collerek.github.io/ormar/relations/foreign-key/#add
To Reproduce
Error: ormar.exceptions.ModelPersistenceError: You cannot update not saved model! Use save or upsert method.
(Note: this should be a complete and concise piece of code that allows reproduction of an issue)
Expected behavior
A clear and concise description of what you expected to happen.
Screenshots
If applicable, add screenshots to help explain your problem.
Versions (please complete the following information):
ormar
0.10.9pydantic
versionAdditional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: