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

Reverse relation not returned from get_pydantic #1215

Open
tscelsi opened this issue Oct 5, 2023 · 0 comments
Open

Reverse relation not returned from get_pydantic #1215

tscelsi opened this issue Oct 5, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@tscelsi
Copy link

tscelsi commented Oct 5, 2023

Describe the bug
Hi! I am trying to get some ormar model tests up and running and to test the foreign key relation and reverse-relation functionality. Currently I have a very simple test file copied mainly from the ormar tests themselves. It looks something like this:

from test.settings import DATABASE_URL
from typing import Optional

import databases
import ormar
import sqlalchemy

metadata = sqlalchemy.MetaData()
database = databases.Database(DATABASE_URL, force_rollback=True)


class BaseMeta(ormar.ModelMeta):
    metadata = metadata
    database = database


class Person(ormar.Model):
    class Meta(BaseMeta):
        tablename = "person"

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


class Pet(ormar.Model):
    class Meta(BaseMeta):
        tablename = "pet"

    id: int = ormar.Integer(primary_key=True)
    name: str = ormar.String(max_length=50)
    owner: Person = ormar.ForeignKey(Person, ondelete="CASCADE")


def test_fields_exist():
    assert 'owner' in Pet.get_pydantic().__fields__
    assert 'pets' in Person.get_pydantic().__fields__

My test simply asserts that both sides of the relation exist. That is that a pet has an owner and that a person can have some pets. Unfortunately, this test fails on the second assertion, there is no 'pets' pydantic field when calling get_pydantic() for Person. Maybe this is expected behaviour but some clarification on how it works would be nice (because I could have sworn that sometimes the 'pets' field appears when using get_pydantic).

To Reproduce
Steps to reproduce the behavior:

  1. Copy above code, or look at test suite of ormar
  2. run the test_fields_exist test

(Note: this should be a complete and concise piece of code that allows reproduction of an issue)

Expected behavior
Person get_pydantic() model should have a field 'pets' for the reverse-relation of the Pet foreign key.

Versions (please complete the following information):

  • Database backend used (mysql/sqlite/postgress): aiosqlite v0.19.0
  • Python version: 3.10.5
  • ormar version: 0.12.2
  • pydantic version: 1.10.8
@tscelsi tscelsi added the bug Something isn't working label Oct 5, 2023
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

No branches or pull requests

1 participant