diff --git a/docs/releases.md b/docs/releases.md index 82ae430..ba34c2f 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -1,3 +1,9 @@ +# 0.10.8 + +## 🐛 Fixes + +* Fix populating default values in pk_only child models [#202](https://github.com/collerek/ormar/issues/202) + # 0.10.7 ## ✨ Features diff --git a/ormar/__init__.py b/ormar/__init__.py index be40cdd..0540e46 100644 --- a/ormar/__init__.py +++ b/ormar/__init__.py @@ -76,7 +76,7 @@ class UndefinedType: # pragma no cover Undefined = UndefinedType() -__version__ = "0.10.7" +__version__ = "0.10.8" __all__ = [ "Integer", "BigInteger", diff --git a/ormar/models/newbasemodel.py b/ormar/models/newbasemodel.py index f94d0f1..c2a9476 100644 --- a/ormar/models/newbasemodel.py +++ b/ormar/models/newbasemodel.py @@ -132,11 +132,15 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass new_kwargs, through_tmp_dict = self._process_kwargs(kwargs) - values, fields_set, validation_error = pydantic.validate_model( - self, new_kwargs # type: ignore - ) - if validation_error and not pk_only: - raise validation_error + if not pk_only: + values, fields_set, validation_error = pydantic.validate_model( + self, new_kwargs # type: ignore + ) + if validation_error: + raise validation_error + else: + fields_set = {self.Meta.pkname} + values = new_kwargs object.__setattr__(self, "__dict__", values) object.__setattr__(self, "__fields_set__", fields_set) diff --git a/tests/test_fastapi/test_relations_with_nested_defaults.py b/tests/test_fastapi/test_relations_with_nested_defaults.py new file mode 100644 index 0000000..d0d11c8 --- /dev/null +++ b/tests/test_fastapi/test_relations_with_nested_defaults.py @@ -0,0 +1,120 @@ +from typing import Optional + +import databases +import pytest +import sqlalchemy +from fastapi import FastAPI +from starlette.testclient import TestClient + +import ormar +from tests.settings import DATABASE_URL + +database = databases.Database(DATABASE_URL) +metadata = sqlalchemy.MetaData() + +app = FastAPI() +app.state.database = database + + +@app.on_event("startup") +async def startup() -> None: + database_ = app.state.database + if not database_.is_connected: + await database_.connect() + + +@app.on_event("shutdown") +async def shutdown() -> None: + database_ = app.state.database + if database_.is_connected: + await database_.disconnect() + + +class BaseMeta(ormar.ModelMeta): + metadata = metadata + database = database + + +class Country(ormar.Model): + class Meta(BaseMeta): + tablename = "countries" + + id: int = ormar.Integer(primary_key=True) + name: str = ormar.String(max_length=100, default="Poland") + + +class Author(ormar.Model): + class Meta(BaseMeta): + tablename = "authors" + + id: int = ormar.Integer(primary_key=True) + name: str = ormar.String(max_length=100) + rating: int = ormar.Integer(default=0) + country: Optional[Country] = ormar.ForeignKey(Country) + + +class Book(ormar.Model): + class Meta(BaseMeta): + tablename = "books" + + id: int = ormar.Integer(primary_key=True) + author: Optional[Author] = ormar.ForeignKey(Author) + title: str = ormar.String(max_length=100) + year: int = ormar.Integer(nullable=True) + + +@pytest.fixture(autouse=True, scope="module") +def create_test_database(): + engine = sqlalchemy.create_engine(DATABASE_URL) + metadata.create_all(engine) + yield + metadata.drop_all(engine) + + +@pytest.fixture() +async def sample_data(): + country = await Country(id=1, name="USA").save() + author = await Author(id=1, name="bug", rating=5, country=country).save() + await Book( + id=1, author=author, title="Bug caused by default value", year=2021 + ).save() + + +@app.get("/books/{book_id}", response_model=Book) +async def get_book_by_id(book_id: int): + book = await Book.objects.get(id=book_id) + return book + + +@app.get("/books_with_author/{book_id}", response_model=Book) +async def get_book_with_author_by_id(book_id: int): + book = await Book.objects.select_related("author").get(id=book_id) + return book + + +def test_related_with_defaults(sample_data): + client = TestClient(app) + with client as client: + response = client.get("/books/1") + assert response.json() == { + "author": {"id": 1}, + "id": 1, + "title": "Bug caused by default value", + "year": 2021, + } + + response = client.get("/books_with_author/1") + assert response.json() == { + "author": { + "books": [ + {"id": 1, "title": "Bug caused by default value", "year": 2021} + ], + "country": {"id": 1}, + "id": 1, + "name": "bug", + "rating": 5, + }, + "id": 1, + "title": "Bug caused by default value", + "year": 2021, + }