Skip to content

Conversation

@bolu61
Copy link

@bolu61 bolu61 commented Oct 29, 2024

see #229 (comment)

I'm not really sure how Pydantic does it, but here's a potential workaround to the problem.

@svlandeg svlandeg added the feature New feature or request label Feb 24, 2025
@svlandeg svlandeg changed the title Add PEP 593 support for Requirement annotations ✨ Add PEP 593 support for Requirement annotations Feb 24, 2025
Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bolu61, thanks for working on this!

So, this PR fixes the following code example:

class Foo(SQLModel, table=True):
    id: Annotated[int | None, Field(primary_key=True)] = None
    uuid: Annotated[UUID, Field(unique=True)]

class Bar(SQLModel, table=True):
    id: Annotated[int | None, Field(primary_key=True)] = None
    uuid: Annotated[UUID, Field(unique=True)]
    foo_id: Annotated[int, Field(foreign_key="foo.id")]
    foo: Annotated[Foo, Relationship()]

For now last line (with Relationship()) doesn't work (ValueError: <class '__main__.Foo'> has no matching SQLAlchemy type) and this PR fixes that.

I think this is not the same that was initially proposed in #229. The idea there was to support passing multiple annotations like:

class Hero(SQLModel, table=True):
    id: Annotated[Optional[int], Field(examples=....), Column(primary_key=True)] = None

Where Field can be pydantic.Field.
So, I suggest we unmark this PR as the one that resolves that issue.

As for changes introduced by this PR, I think it goes in line with modern recommendations to use Annotated approach instead parameterizing fields via default value.
I think we should test this approach more and add some automatic tests. Then it will have all chances to be accepted.

@bolu61
Copy link
Author

bolu61 commented Sep 11, 2025

@YuriiMotov I agree.

I'm not too familiar with this codebase. Can you point me to where I can write those tests?

@YuriiMotov
Copy link
Member

I'm not too familiar with this codebase. Can you point me to where I can write those tests?

You can create an annotated version of the code example from "Relationships" section of docs:

@github-actions
Copy link
Contributor

github-actions bot commented Nov 4, 2025

Heads-up: this will be closed in 3 days unless there’s new activity.

@bolu61
Copy link
Author

bolu61 commented Nov 7, 2025

The test is failing with:

FAILED tests/test_tutorial/test_relationship_attributes/test_define_relationship_attributes/test_tutorial001_an_py310.py::test_tutorial - sqlalchemy.exc.ArgumentError: Mapper Mapper[Team(team)] could not assemble any primary key columns for mapped table 'team'

I'm not exactly sure how to approach this.

@github-actions github-actions bot removed the waiting label Nov 7, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 7, 2025

📝 Docs preview

Last commit 40af690 at: https://c0d7bffc.sqlmodel.pages.dev

@YuriiMotov
Copy link
Member

The test is failing with:

FAILED tests/test_tutorial/test_relationship_attributes/test_define_relationship_attributes/test_tutorial001_an_py310.py::test_tutorial - sqlalchemy.exc.ArgumentError: Mapper Mapper[Team(team)] could not assemble any primary key columns for mapped table 'team'

I'm not exactly sure how to approach this.

This is related to #1623 . I hope it will be fixed soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants