Skip to content

support dataclass defaults that don't affect subsequent ORM operations #12168

Closed

Description

@CaselIT

The use case for this is mainly dataclass defaults, so that we can use mapped_column(default=<new-constrant-here>) and that's equivalent to not setting the field at all.

This should also work for relationships:

Some considerations from other discussions:

i think one area that we have a choice to make is if you have an object with state and you say, myobject.foo = NO_VALUE - does that expire the attribute or just do nothing? I vote for the latter
#12165 (reply in thread)

I guess the behaviour wanted is

thing = session.get(Thing, 1)
assert thing.foo == 'bar'
thing.foo = NO_VALUE
assert thing.foo == 'bar'

and if foo is expired it stays expired, so

thing = session.get(Thing, 1)
session.expire(thing, 'foo')
assert 'foo' in sa.inspect(x).expired_attributes
thing.foo = NO_VALUE
assert 'foo' in sa.inspect(x).expired_attributes

In the above example either behaviour is fine.
Not sure if before assert thing.foo == 'bar' in the first example 'foo' should be in the expired_attributes or not. I trust your insight here if you prefer for not.
#12165 (reply in thread)

Connected discussions:

Target is 2.0.x but may be moved to 2.1 depending on the changes

Activity

  1. added
    use case
    on Dec 9, 2024
  2. added this to the 2.0.x milestone on Dec 9, 2024
  3. CaselIT commented on Dec 9, 2024

    @CaselIT
    MemberAuthor

    I prefer UNSET compared with NO_VALUE as a word, but overall they mean probably the same thing

  4. CaselIT commented on Dec 9, 2024

    @CaselIT
    MemberAuthor

    myobject.foo = NO_VALUE - does that expire the attribute or just do nothing

    Another case:

    thing = session.get(Thing, 1)
    assert thing.foo == 'bar'
    thing.foo = 'new-value'
    thing.foo = NO_VALUE
    # assert thing.foo == 'bar' # this would again pass
    session.commit()  # nothing to do here. No update is done, new-value is lost

    Here I think the correct behaviour would be to ignore the set of new-value, so I would go for expire, since I'm not sure if it can be done without expiring the field. If we can do that that I'm back ad no preference

  5. changed the title [-]Add a magic value for the ORM to indicate that no-value is being set.[/-] [+]Add a constant for the ORM to indicate that no-value is being set.[/+] on Dec 9, 2024
  6. zzzeek commented on Dec 9, 2024

    @zzzeek
    Member

    as mentioned elsewhere there's not a great way to type this

    if you say

    field: Mapped[str | None] = mapped_column(default=DONTSET)
    

    That's going to fail typing, would have to be like

    field: Mapped[str | None | DontSetType] = mapped_column(default=DONTSET)
    

    unless if we typed DONTSET as None .....

  7. zzzeek commented on Dec 9, 2024

    @zzzeek
    Member

    so that typing looks like:

    if TYPE_CHECKING:
        DONTSET = None
    else:
        DONTSET = object()
    
    class UserMappedModel(MappedAsDataclass, Base):
        __tablename__ = "user"
    
        id: Mapped[int] = mapped_column(primary_key=True)
        name: Mapped[str]
        status: Mapped[str | None] = mapped_column(default=DONTSET)

    we also have a way to customize that default "scalar get" value which is little used : https://docs.sqlalchemy.org/en/20/orm/events.html#sqlalchemy.orm.AttributeEvents.init_scalar

    to use that, someone would need to make another DONTSET of their own, like:

    if TYPE_CHECKING:
        DONTSET_MY_DEFAULT = "default"
    else:
        DONTSET_MY_DEFAULT = DONTSET
    
    class UserMappedModel(MappedAsDataclass, Base):
        __tablename__ = "user"
    
        id: Mapped[int] = mapped_column(primary_key=True)
        name: Mapped[str]
        status: Mapped[str] = mapped_column(default=DONTSET_MY_DEFAULT)
  8. zzzeek commented on Dec 9, 2024

    @zzzeek
    Member

    maybe we have a bunch of them canned for typical defaults

    if TYPE_CHECKING:
        DONTSET_NONE = None
        DONTSET_STR = "string"
        DONTSET_INT = 0
        # ... etc
    
    else:
        _DONTSET = object()
        DONTSET_NONE = _DONTSET
        DONTSET_STR = _DONTSET
        DONTSET_INT = _DONTSET
        # ...
  9. zzzeek commented on Dec 9, 2024

    @zzzeek
    Member

    this does the basic thing

    diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py
    index d65597238..9c47d9553 100644
    --- a/lib/sqlalchemy/orm/attributes.py
    +++ b/lib/sqlalchemy/orm/attributes.py
    @@ -45,6 +45,7 @@ from .base import ATTR_EMPTY
     from .base import ATTR_WAS_SET
     from .base import CALLABLES_OK
     from .base import DEFERRED_HISTORY_LOAD
    +from .base import DONT_SET
     from .base import INCLUDE_PENDING_MUTATIONS  # noqa
     from .base import INIT_OK
     from .base import instance_dict as instance_dict
    @@ -1268,6 +1269,9 @@ class _ScalarAttributeImpl(_AttributeImpl):
             check_old: Optional[object] = None,
             pop: bool = False,
         ) -> None:
    +        if value is DONT_SET:
    +            return
    +
             if self.dispatch._active_history:
                 old = self.get(state, dict_, PASSIVE_RETURN_NO_VALUE)
             else:
    @@ -1434,6 +1438,9 @@ class _ScalarObjectAttributeImpl(_ScalarAttributeImpl):
         ) -> None:
             """Set a value on the given InstanceState."""
     
    +        if value is DONT_SET:
    +            return
    +
             if self.dispatch._active_history:
                 old = self.get(
                     state,
    diff --git a/lib/sqlalchemy/orm/base.py b/lib/sqlalchemy/orm/base.py
    index b5f7dbbaf..948ed0bed 100644
    --- a/lib/sqlalchemy/orm/base.py
    +++ b/lib/sqlalchemy/orm/base.py
    @@ -97,6 +97,7 @@ class LoaderCallableStatus(Enum):
     
         """
     
    +    DONT_SET = 5
     
     (
         PASSIVE_NO_RESULT,
    @@ -104,6 +105,7 @@ class LoaderCallableStatus(Enum):
         ATTR_WAS_SET,
         ATTR_EMPTY,
         NO_VALUE,
    +    DONT_SET,
     ) = tuple(LoaderCallableStatus)
     
     NEVER_SET = NO_VALUE
    

    demo

    from __future__ import annotations
    
    from sqlalchemy import create_engine
    from sqlalchemy import ForeignKey
    from sqlalchemy.orm import DeclarativeBase
    from sqlalchemy.orm import Mapped
    from sqlalchemy.orm import mapped_column
    from sqlalchemy.orm import relationship
    from sqlalchemy.orm import Session, MappedAsDataclass
    from sqlalchemy.orm.attributes import DONT_SET
    from typing import TYPE_CHECKING
    
    class Base(DeclarativeBase):
        pass
    
    if TYPE_CHECKING:
        DONTSET_NONE = None
    
    else:
        DONTSET_NONE = DONT_SET
    
    class UserMappedModel(MappedAsDataclass, Base):
        __tablename__ = "user"
    
        id: Mapped[int] = mapped_column(primary_key=True)
        name: Mapped[str]
        status: Mapped[str | None] = mapped_column(default=DONTSET_NONE)
    
    e = create_engine("sqlite://")
    Base.metadata.create_all(e)
    
    session = Session(e)
    
    user = UserMappedModel(id=1, name="Jon", status="active")
    session.merge(user)
    session.commit()
    
    
    user = UserMappedModel(id=1, name="John")
    assert user.status is None
    new_user = session.merge(user)
    session.commit()
    
    print(new_user)

    there can be the possibility of doing more with the init_scalar event and having this None be more customizable

  10. CaselIT commented on Dec 9, 2024

    @CaselIT
    MemberAuthor

    as mentioned elsewhere there's not a great way to type this

    We can just type this as Any. That eliminates any typing issue

  11. zzzeek commented on Dec 9, 2024

    @zzzeek
    Member

    @n-leonovets I can see doing the above pattern, integrating init_scalar, and having the two flags such as:

    class UserMappedModel(MappedAsDataclass, Base):
        __tablename__ = "user"
    
        id: Mapped[int] = mapped_column(primary_key=True)
        name: Mapped[str]
        status: Mapped[str | None] = mapped_column(default=None, stealth_default=True)

    what it means is that making a UserMappedModel will not actually populate None into __dict__ if not passed, that is, it still is using DONT_SET above, you just dont see it.

    stealth_default could set up an event to support other kinds of defaults too other than None, this is what init_scalar supports

    here is that

    from __future__ import annotations
    
    from sqlalchemy import create_engine
    from sqlalchemy import event
    from sqlalchemy import ForeignKey
    from sqlalchemy.orm import DeclarativeBase
    from sqlalchemy.orm import Mapped
    from sqlalchemy.orm import mapped_column
    from sqlalchemy.orm import relationship
    from sqlalchemy.orm import Session, MappedAsDataclass
    from sqlalchemy.orm.attributes import DONT_SET
    from typing import TYPE_CHECKING
    
    class Base(DeclarativeBase):
        pass
    
    
    class UserMappedModel(MappedAsDataclass, Base):
        __tablename__ = "user"
    
        id: Mapped[int] = mapped_column(primary_key=True)
        name: Mapped[str]
    
        if TYPE_CHECKING:
            # what the API would be
            status: Mapped[str] = mapped_column(default="default_status", stealth_default=True)
        else:
            # what actually happens, part I
            status: Mapped[str] = mapped_column(default=DONT_SET)
    
    # what actually happens, part II
    @event.listens_for(UserMappedModel.status, "init_scalar", retval=True)
    def receive_init_scalar(target, value, dict_):
        return "default_status"
    
    e = create_engine("sqlite://")
    Base.metadata.create_all(e)
    
    session = Session(e)
    
    user = UserMappedModel(id=1, name="Jon", status="active")
    session.merge(user)
    session.commit()
    
    
    user = UserMappedModel(id=1, name="John")
    assert user.status == "default_status"
    
    new_user = session.merge(user)
    session.commit()
    
    print(new_user)
  12. CaselIT commented on Dec 9, 2024

    @CaselIT
    MemberAuthor

    I'm -1 on doing that under the hood, but that new argument may make sense to add. But I think it's better to just pass to it the value while keeping default as is.

    So something like mapped_column(default=UNSET, new_argument="default_status")

  13. nleononline commented on Dec 10, 2024

    @nleononline

    @n-leonovets Я могу представить себе выполнение вышеуказанного шаблона, интеграцию init_scalarи наличие двух флагов, например:

    class UserMappedModel(MappedAsDataclass, Base):
        __tablename__ = "user"
    
        id: Mapped[int] = mapped_column(primary_key=True)
        name: Mapped[str]
        status: Mapped[str | None] = mapped_column(default=None, stealth_default=True)

    это означает, что создание UserMappedModelволи фактически не заполнит Noneполе __dict__if not passed, то есть оно все равно используется DONT_SETвыше, вы просто его не видите.

    stealth_default может настроить событие для поддержки других типов значений по умолчанию None, кроме того, что поддерживает init_scalar

    вот это

    from __future__ import annotations
    
    from sqlalchemy import create_engine
    from sqlalchemy import event
    from sqlalchemy import ForeignKey
    from sqlalchemy.orm import DeclarativeBase
    from sqlalchemy.orm import Mapped
    from sqlalchemy.orm import mapped_column
    from sqlalchemy.orm import relationship
    from sqlalchemy.orm import Session, MappedAsDataclass
    from sqlalchemy.orm.attributes import DONT_SET
    from typing import TYPE_CHECKING
    
    class Base(DeclarativeBase):
        pass
    
    
    class UserMappedModel(MappedAsDataclass, Base):
        __tablename__ = "user"
    
        id: Mapped[int] = mapped_column(primary_key=True)
        name: Mapped[str]
    
        if TYPE_CHECKING:
            # what the API would be
            status: Mapped[str] = mapped_column(default="default_status", stealth_default=True)
        else:
            # what actually happens, part I
            status: Mapped[str] = mapped_column(default=DONT_SET)
    
    # what actually happens, part II
    @event.listens_for(UserMappedModel.status, "init_scalar", retval=True)
    def receive_init_scalar(target, value, dict_):
        return "default_status"
    
    e = create_engine("sqlite://")
    Base.metadata.create_all(e)
    
    session = Session(e)
    
    user = UserMappedModel(id=1, name="Jon", status="active")
    session.merge(user)
    session.commit()
    
    
    user = UserMappedModel(id=1, name="John")
    assert user.status == "default_status"
    
    new_user = session.merge(user)
    session.commit()
    
    print(new_user)

    looks a little strange and not obvious.
    I think it's worth striving for standard model behavior, regardless of whether MappedAsDataclass is used or not.

    adding event.listens_for is not obvious and different behavior from the base model.

    It seems to me that it is worth finalizing this proposed solution #12165 (reply in thread), because... it is more obvious and can be hidden in alchemy so that users do not have to learn a new approach to alchemy, which is already not obvious in many places

  14. 24 remaining items

  15. zzzeek commented on May 19, 2025

    @zzzeek
    Member

    Here is a custom base that solves the problem for 2.0

    class Base(DeclarativeBase):
        def __post_init__(self):
            for m2o_rel in (
                r
                for r in inspect(self).mapper.relationships
                if r.direction is MANYTOONE
            ):
                if self.__dict__.get(m2o_rel.key, False) is None:
                    self.__dict__.pop(m2o_rel.key, None)

    POC

    from __future__ import annotations
    
    import uuid
    
    from sqlalchemy import create_engine
    from sqlalchemy import engine
    from sqlalchemy import ForeignKey
    from sqlalchemy import inspect
    from sqlalchemy import String
    from sqlalchemy.orm import DeclarativeBase
    from sqlalchemy.orm import MANYTOONE
    from sqlalchemy.orm import Mapped
    from sqlalchemy.orm import mapped_column
    from sqlalchemy.orm import MappedAsDataclass
    from sqlalchemy.orm import relationship
    from sqlalchemy.orm import Session
    
    
    class Base(DeclarativeBase):
        def __post_init__(self):
            for m2o_rel in (
                r
                for r in inspect(self).mapper.relationships
                if r.direction is MANYTOONE
            ):
                if self.__dict__.get(m2o_rel.key, False) is None:
                    self.__dict__.pop(m2o_rel.key, None)
    
    
    class Tenant(MappedAsDataclass, Base):
        __tablename__ = "tenant"
    
        id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
    
    
    class User(MappedAsDataclass, Base):
        __tablename__ = "user"
    
        id: Mapped[int] = mapped_column(primary_key=True, init=False)
        name: Mapped[str] = mapped_column(String(30))
    
        tenant_id: Mapped[uuid.UUID | None] = mapped_column(
            ForeignKey("tenant.id", onupdate="cascade"), default=None
        )
        tenant: Mapped["Tenant"] = relationship("Tenant", default=None)
    
    
    engine = create_engine("sqlite://")
    
    Base.metadata.create_all(engine)
    
    with Session(engine) as session:
        tenant = Tenant(id=uuid.uuid4())
    
        user_a = User(name="user_a", tenant_id=tenant.id)
    
        user_b = User(name="user_b", tenant=tenant)
        session.add(user_a)
        session.add(user_b)
        session.commit()
    
        a = session.get(User, user_a.id)
        b = session.get(User, user_b.id)
        print(a.tenant_id)
        print(b.tenant_id)
  16. zzzeek commented on Jun 20, 2025

    @zzzeek
    Member

    we're going to expand this to include default_factory with collection-based relationship

  17. reopened this on Jun 20, 2025
  18. zzzeek commented on Jun 20, 2025

    @zzzeek
    Member

    here's the mcve for the relationship case

    from sqlalchemy import ForeignKey, create_engine
    from sqlalchemy.orm import (
        DeclarativeBase,
        Mapped,
        MappedAsDataclass,
        Session,
        mapped_column,
        relationship,
    )
    
    # Create engine
    engine = create_engine("sqlite:///:memory:")  # echo=True
    
    
    class Base(MappedAsDataclass, DeclarativeBase, kw_only=True):
        pass
    
    
    class Parent(Base):
        __tablename__ = "parents"
    
        id: Mapped[int] = mapped_column(primary_key=True, init=False)
        name: Mapped[str]
    
        children: Mapped[list["Child"]] = relationship(
            back_populates="parent",
            cascade="all, delete-orphan",
            default_factory=list,  # Using `init=False` instead will result in 2 children, even after merge
        )
    
    
    class Child(Base):
        __tablename__ = "children"
    
        id: Mapped[int] = mapped_column(primary_key=True, init=False)
        name: Mapped[str]
        parent_id: Mapped[int] = mapped_column(
            ForeignKey("parents.id", ondelete="CASCADE"), init=False
        )
    
        parent: Mapped[Parent] = relationship(
            back_populates="children", init=False
        )
    
    
    Base.metadata.create_all(engine)
    
    # Create a parent with some children.
    with Session(engine) as session:
        # Create a parent with two children
        parent = Parent(name="Parent1")
        parent.children.append(Child(name="Child1"))
        parent.children.append(Child(name="Child2"))
        session.add(parent)
        session.commit()
    
        print("Original parent child count:", len(parent.children))  # 2
    
    # When merging in a new Parent instance with the same ID, the `default_factory=list` causes the
    # attribute to be "loaded", leading all children to be deleted.
    with Session(engine) as session:
        new_parent = Parent(name="Parent1")
        new_parent.id = parent.id
        merged_parent = session.merge(new_parent)
    
        # After merging, the children will be deleted due to the default_factory.
        print("Child count after merge:", len(merged_parent.children))  # 0!
  19. sqla-tester commented on Jun 20, 2025

    @sqla-tester
    Collaborator

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

    Extend #12168 to relationship collections https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5977

  20. added a commit that references this issue on Jun 23, 2025
  21. added a commit that references this issue on Jun 24, 2025
  22. added a commit that references this issue on Jul 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dataclass relationship defaults as Nonedataclassesormuse case

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions