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

Document Source Code Reverse Relation Add Child Raise Error #216

Closed
fourcels opened this issue Jun 2, 2021 · 1 comment · Fixed by #217
Closed

Document Source Code Reverse Relation Add Child Raise Error #216

fourcels opened this issue Jun 2, 2021 · 1 comment · Fixed by #217
Labels
bug Something isn't working

Comments

@fourcels
Copy link

fourcels commented Jun 2, 2021

Describe the bug

https://collerek.github.io/ormar/relations/foreign-key/#add

department = await Department(name="Science").save()
course = Course(name="Math", completed=False) # note - not saved

await department.courses.add(course)
assert course.pk is not None # child model was saved
# relation on child model is set and FK column saved in db
assert courses.department == department
# relation on parent model is also set
assert department.courses[0] == course 

To Reproduce

from typing import Optional

import databases
import sqlalchemy
import asyncio

import ormar

DATABASE_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 definition
engine = sqlalchemy.create_engine(DATABASE_URL)
# note that this has to be the same metadata that is used in ormar Models definition


class Department(ormar.Model):
    class Meta:
        database = database
        metadata = metadata

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=100)


class Course(ormar.Model):
    class Meta:
        database = database
        metadata = metadata

    id: 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)


async def run():
    await database.connect()
    department = await Department(name="Science").save()
    course = Course(name="Math", completed=False)  # note - not saved

    await department.courses.add(course)
    await database.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.

@collerek
Copy link
Owner

collerek commented Jun 2, 2021

Thanks for catching that - fixed in 0.10.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants