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 sqlalchemy MappedColumn #1143

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

spazm
Copy link

@spazm spazm commented Oct 21, 2024

Reopening PR #896.

id: Optional[int] = Field(default=None, sa_column=mapped_column('id', Integer, primary_key=True))

This PR adds a test with sqlmodel.orm.mapped_column working for sa_column. This test fails on current main.

from typing import Optional
from sqlalchemy import Integer
from sqlalchemy.orm import mapped_column
from sqlmodel import Field

class Foo(SqlModel):
    id: Optional[int] = Field(default=None, sa_column=mapped_column('id', Integer, primary_key=True))

mavwolverine and others added 4 commits October 21, 2024 15:59

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
These tests share the same structure and field names, so
they conflict if not cleaned up.
@spazm spazm changed the title Support MappedColumn for sa_column add: Support MappedColumn for sa_column Oct 29, 2024
@spazm spazm changed the title add: Support MappedColumn for sa_column add: Support sqlalchemy MappedColumn for sa_column Oct 29, 2024
@spazm
Copy link
Author

spazm commented Oct 29, 2024

This PR is failing one test because it does not have a label assigned.
How do I get a label assigned?

Do I need to open a separate issue to point to the PR?

isinstance directly supports using a tuple of allowed types.
@lachaib
Copy link

lachaib commented Feb 17, 2025

Hello,

While trying to fix a problem probably similar to yours (I want to make a column deferrable but it can only be made on mapped_column and not on Column), I tested a simpler solution:

at the end of get_column_from_field, I returned mapped_column instead of Column, hence the deferred keyword can be passed as sa_column_kwargs

According to tests, this is fully compatible. Also it's slightly more readable:

not_loaded: str = Field(sa_column_kwargs={"deferred": True})
# vs
not_loaded: str = Field(sa_type=mapped_column(String, deferred=True))

Maybe there's an underlying reason why this cannot be acceptable but I think it's worth sharing (and also will help me follow this pull request)

@svlandeg svlandeg added the feature New feature or request label Feb 20, 2025
@svlandeg svlandeg changed the title add: Support sqlalchemy MappedColumn for sa_column ✨ Support sqlalchemy MappedColumn Feb 20, 2025
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.

None yet

4 participants