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

✨ Support sqlmodel_rebuild #1270

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft

Conversation

adsharma
Copy link

@adsharma adsharma commented Jan 17, 2025

The @sqlmodel decorator introduced by fquery.sqlmodel moves schema definitions and types closer to the object model (references) instead of foreign_keys.

Instead of writing:

class Hero(SQLModel, table=True):
    ...
    team_id: int | None = Field(default=None, foreign_key="team.id")

you could write:

@sqlmodel
class Hero:
    ...
    team: Team | None = foreign_key("team.id")

Everything works the same as before. Only the syntax has changed. However, if Team also has a foreign key to Hero, then one of them will have to use forward declaration so type checkers work.

In those cases we need a two pass solution where in the second pass, with the full type definition available, we can generate the correct code.

Refactor some of the existing code to introduce sqlmodel_rebuild().

@adsharma adsharma marked this pull request as draft January 17, 2025 01:27
@adsharma adsharma marked this pull request as ready for review January 17, 2025 04:56
@svlandeg svlandeg marked this pull request as draft February 24, 2025 10:39
@svlandeg svlandeg changed the title Support sqlmodel_rebuild, similar to pydantic ✨ Support sqlmodel_rebuild Feb 24, 2025
@svlandeg svlandeg added the feature New feature or request label Feb 24, 2025
@adsharma adsharma marked this pull request as ready for review February 24, 2025 18:44
Linter doesn't understand the special call handling for this case.
@adsharma
Copy link
Author

@svlandeg I think there were some merge errors in one of the commits in this thread.

The following should show changes to only sqlmodel/main.py. However, I see other deltas.

git diff 83629f6 7d00768 --stat
 .github/workflows/publish.yml   |  2 +-
 .github/workflows/smokeshow.yml | 12 +-----------
 .github/workflows/test.yml      | 11 ++---------
 docs/databases.md               |  2 +-
 docs/release-notes.md           |  6 ------
 sqlmodel/main.py                | 17 +----------------
 6 files changed, 6 insertions(+), 44 deletions(-)

@svlandeg
Copy link
Member

svlandeg commented Feb 27, 2025

@svlandeg I think there were some merge errors in one of the commits in this thread.

The following should show changes to only sqlmodel/main.py. However, I see other deltas.

The diff on this PR only shows changes in sqlmodel/main.py. That said, the CI has not been green at any point throughout the commits of this PR. This should be fixed before the PR is taken out of draft and marked as ready for review.

As you can see, the tests for Pydantic v1 are failing. This needs to be addressed before we can properly review this PR. Thanks! 🙏

@svlandeg svlandeg marked this pull request as draft February 27, 2025 08:35
@adsharma
Copy link
Author

The diff on this PR only shows changes in sqlmodel/main.py

That's the inconsistency I'm calling out. What the UI shows and git diff commit1 commit2 show are different and I believe that's causing the test failures.

I'm thinking I'll close this and create a new PR as a workaround.

@adsharma
Copy link
Author

I looked into the pydantic_v1 test failures a bit. There are 3 different instances of the test:

~/src/sqlmodel/docs_src/tutorial/fastapi/app_testing$ ls -1d tut*
tutorial001
tutorial001_py310
tutorial001_py39

If I run pytest in each individual directory, they all pass. However, if I try to turn pytest in the parent directory, it tries to load the Hero class from tutorial001 and then again an identically named class in tutorial001_py310 and fails exactly the same way as CI. Please note that this is happening at test collection time, even before tests are executed.

I can reproduce this behavior on the main branch as well. So the question becomes how were these tests passing before my changes?

@svlandeg
Copy link
Member

I can reproduce this behavior on the main branch as well. So the question becomes how were these tests passing before my changes?

Hm, that's puzzling to me as well, as the tests do seem to succeed just fine on other recent PRs for this repo 🤔

@adsharma
Copy link
Author

@svlandeg the above test run verifies that the pydantic-v1 breakage was caused by my change.

Looking into the actual cause of the failure, I think there is some __call__ magic happening on the call to __do_init__() that I don't completely understand. The problem is that classname argument is different before and after the change.

Solving this boils down to figuring out how to correctly invoke __do_init__() so the behavior is the same as before.

@adsharma
Copy link
Author

adsharma commented Mar 1, 2025

@svlandeg tests are all green. Please review.

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.

2 participants