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

Cannot override index=True with mapped_column in Annotated #11091

Closed
zzzeek opened this issue Mar 4, 2024 Discussed in #11090 · 2 comments
Closed

Cannot override index=True with mapped_column in Annotated #11091

zzzeek opened this issue Mar 4, 2024 Discussed in #11090 · 2 comments
Labels
bug Something isn't working great mcve An issue with a great mcve orm - annotated declarative issues with the new annotations-based declarative ORM approach orm
Milestone

Comments

@zzzeek
Copy link
Member

zzzeek commented Mar 4, 2024

Discussed in #11090

Originally posted by decardev March 4, 2024
Cannot override mapped_column(index=True) when using Annotated

python 3.12
OK: OSX
sqlalchemy: 2.0.27

The documentation states:

The mapped_column() construct is also reconciled with an explicitly passed mapped_column() construct, whose arguments will take precedence over those of the Annotated construct. Below we add a ForeignKey constraint to our integer primary key and also use an alternate server default for the created_at column:

But the following produces an indexe for fail.

from typing import Annotated

from pydantic.dataclasses import dataclass
from sqlalchemy import Integer, inspect
from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass, mapped_column


class Base(MappedAsDataclass, DeclarativeBase, dataclass_callable=dataclass):
    pass


IdField = Annotated[int, mapped_column(Integer, index=False)]
IdFieldIndexed = Annotated[int, mapped_column(Integer, index=True)]


class Model(Base):
    __tablename__ = "model"
    id: Mapped[int] = mapped_column(Integer, primary_key=True, default=1)
    ok_default: Mapped[IdField] = mapped_column(default=1)
    ok_overrides_false: Mapped[IdField] = mapped_column(index=True, default=1)

    fail: Mapped[IdFieldIndexed] = mapped_column(index=False, default=1)


# mapped_column does not override the index parameter of the Annotated type when True. 
{name: col.expression.index for name, col in inspect(Model).column_attrs.items()}
```</div>
@zzzeek zzzeek added bug Something isn't working orm orm - annotated declarative issues with the new annotations-based declarative ORM approach labels Mar 4, 2024
@zzzeek zzzeek added this to the 2.0.x milestone Mar 4, 2024
@zzzeek zzzeek added the great mcve An issue with a great mcve label Mar 4, 2024
@sqla-tester
Copy link
Collaborator

Mike Bayer has proposed a fix for this issue in the main branch:

accommodate False conditions for unique / index merge https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5178

@sqla-tester
Copy link
Collaborator

Mike Bayer has proposed a fix for this issue in the rel_2_0 branch:

accommodate False conditions for unique / index merge https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5179

sqlalchemy-bot pushed a commit that referenced this issue Mar 5, 2024
Fixed issue in ORM annotated declarative where using
:func:`_orm.mapped_column()` with an :paramref:`_orm.mapped_column.index`
or :paramref:`_orm.mapped_column.unique` setting of False would be
overridden by an incoming ``Annotated`` element that featured that
parameter set to ``True``, even though the immediate
:func:`_orm.mapped_column()` element is more specific and should take
precedence.  The logic to reconcile the booleans has been enhanced to
accommodate a local value of ``False`` as still taking precedence over an
incoming ``True`` value from the annotated element.

Fixes: #11091
Change-Id: I15cda4a0a07a289015c0a09bbe3ca2849956604e
(cherry picked from commit e4c4bd0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working great mcve An issue with a great mcve orm - annotated declarative issues with the new annotations-based declarative ORM approach orm
Projects
None yet
Development

No branches or pull requests

2 participants