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

exclude argument to load_all does not seem to work consistently #779

Closed
cmflynn opened this issue Aug 11, 2022 · 1 comment
Closed

exclude argument to load_all does not seem to work consistently #779

cmflynn opened this issue Aug 11, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@cmflynn
Copy link

cmflynn commented Aug 11, 2022

Describe the bug
the exclude argument to load_all will prevent related models from loading that are not referenced in the excluded argument.

To Reproduce

import asyncio
import uuid

import ormar
import sqlalchemy
from databases import Database

url = "postgresql://postgres@0.0.0.0:5432/postgres"
database = Database(url=url)
engine = sqlalchemy.create_engine(url)
metadata = sqlalchemy.MetaData()


class BaseMeta:
    metadata = metadata
    database = database


class JimmyUser(ormar.Model):
    class Meta(BaseMeta):
        tablename = "jimmy_users"

    id: uuid.UUID = ormar.UUID(
        primary_key=True, default=uuid.uuid4(), uuid_format="string"
    )


class JimmyProfile(ormar.Model):
    class Meta(BaseMeta):
        tablename = "jimmy_profiles"

    id: uuid.UUID = ormar.UUID(
        primary_key=True, default=uuid.uuid4(), uuid_format="string"
    )
    name = ormar.String(max_length=42, default="JimmyProfile")

    user: JimmyUser = ormar.ForeignKey(to=JimmyUser)


class JimmyAccount(ormar.Model):
    class Meta(BaseMeta):
        tablename = "jimmy_accounts"

    id: uuid.UUID = ormar.UUID(
        primary_key=True, default=uuid.uuid4(), uuid_format="string"
    )

    name = ormar.String(max_length=42, default="JimmyAccount")

    user: JimmyUser = ormar.ForeignKey(to=JimmyUser)

async def main():
    await database.connect()

    metadata.drop_all(bind=engine)
    metadata.create_all(bind=engine)
    user = JimmyUser()
    await user.save()

    await JimmyAccount(user=user).save()
    await JimmyProfile(user=user).save()

    await user.load_all(exclude={"jimmyprofiles"})
    assert hasattr(user.jimmyaccounts[0], "name")
    assert len(user.jimmyprofiles) == 0

    # now flip it... and it breaks?
    metadata.drop_all(bind=engine)
    metadata.create_all(bind=engine)
    user = JimmyUser()
    await user.save()

    await JimmyAccount(user=user).save()
    await JimmyProfile(user=user).save()

    await user.load_all(exclude={"jimmyaccounts"})
    assert await JimmyProfile.objects.get()
    
    # so we know the profile exists, but it is not attached to the user instance, so this line throws an index error 
    assert hasattr(user.jimmyprofiles[0], "name")
    assert len(user.jimmyaccounts) == 0



if __name__ == "__main__":
    asyncio.run(main())

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

Expected behavior
exclude should not prevent un specified related model fields from loading

Versions (please complete the following information):

  • postgres
  • ormar version 0.11.2
@cmflynn cmflynn added the bug Something isn't working label Aug 11, 2022
collerek added a commit that referenced this issue Oct 21, 2022
collerek added a commit that referenced this issue Oct 21, 2022
@collerek
Copy link
Owner

Fixed in 0.12.0, please update and check.

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

2 participants