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

Using Field from a different class causes the validator to wrongly assume there are multiple primary keys #671

Open
sherpya opened this issue Nov 19, 2024 · 6 comments · May be fixed by #672

Comments

@sherpya
Copy link

sherpya commented Nov 19, 2024

the code in RedisModel validate_primary_key wrongly assumes there are multiple primary keys:

            if getattr(field_info, "primary_key", None):
                primary_keys += 1

should really be:

            if getattr(field_info, "primary_key", None) is True:
                primary_keys += 1

because I'm getting PydanticUndefined sometimes instead of None

@sherpya sherpya changed the title Adding an index causes the validator to wrongly assume there are multiple primary keys Using Field from a different class index causes the validator to wrongly assume there are multiple primary keys Nov 19, 2024
@slorello89
Copy link
Member

Interesting, can you share some code where you see this error?

@sherpya
Copy link
Author

sherpya commented Nov 19, 2024

from functools import partial

from sqlmodel import Field

NullableField = partial(Field, default=None, nullable=True)

class SomeModel(HashModel):
    somefield: str | None = NullableField()

@sherpya sherpya changed the title Using Field from a different class index causes the validator to wrongly assume there are multiple primary keys Using Field from a different class causes the validator to wrongly assume there are multiple primary keys Nov 19, 2024
@slorello89
Copy link
Member

This isn't enough to reproduce the issue. Can you please provide a minimal reproduction of the issue.

@sherpya
Copy link
Author

sherpya commented Nov 19, 2024

ok you need at least 2 fields of such type:

from functools import partial

import sqlmodel
from redis_om import HashModel

NullableField = partial(sqlmodel.Field, default=None, nullable=True)


class SomeModel(HashModel):
    somefield1: str | None = NullableField()
    somefield2: str | None = NullableField()


SomeModel()

@slorello89
Copy link
Member

There's a conflict between sqlmodel and redis_om, they both have an attribute primary_key, which redis-om sets to False by default, where sqlmodel sets it to PydanticUndefinied. You can set it to False in your partial there and it should work:

NullableField = partial(sqlmodel.Field, default=None, nullable=True, primary_key=False)

@sherpya
Copy link
Author

sherpya commented Nov 19, 2024

yes it is defined as:

primary_key: Union[bool, UndefinedType] = Undefined

but a check with is True would make more sense for the validator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants