sqlalchemy: support dataclass defaults that don't affect subsequent ORM operations
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 https://github.com/sqlalchemy/sqlalchemy/discussions/12165#discussioncomment-11509551
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
foois expired it stays expired, sothing = 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_attributesIn the above example either behaviour is fine. Not sure if before
assert thing.foo == 'bar'in the first example ‘foo’ should be in theexpired_attributesor not. I trust your insight here if you prefer for not. https://github.com/sqlalchemy/sqlalchemy/discussions/12165#discussioncomment-11510441
Connected discussions:
- https://github.com/sqlalchemy/sqlalchemy/discussions/12165
- https://github.com/sqlalchemy/sqlalchemy/discussions/9383
- https://github.com/sqlalchemy/sqlalchemy/discussions/9856
Target is 2.0.x but may be moved to 2.1 depending on the changes
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 32 (22 by maintainers)
Commits related to this issue
- Merge "Extend #12168 to relationship collections" into main — committed to sqlalchemy/sqlalchemy by zzzeek a year ago
- Extend #12168 to relationship collections As seen in [1] we need to extend the "no default set" case a bit more to help get dataclasses to act more like traditional mapped classes. [1] https://githu... — committed to makesoftwaresafe/sqlalchemy by zzzeek a year ago
- Extend #12168 to relationship collections As seen in [1] we need to extend the "no default set" case a bit more to help get dataclasses to act more like traditional mapped classes. [1] https://githu... — committed to suraj-ora-2020/sqlalchemy by zzzeek a year ago
It seems to me that this is the most adequate way.
There is a simple Default type that you can wrap a value in. We can call self.is_default and if we get True, do not add the value to session methods.
The advantage is that we can hide this construction in the library implementation and get the same behavior for standard values in regular models and
MappedAsDataclass, fixing the original problem https://github.com/sqlalchemy/sqlalchemy/discussions/12165As a result, the code will not be different, are you using a regular model or
MappedAsDataclass:Also, using this approach, we can add an
exclude_defaultorexclude_unsetmethod to themapped_columnand allow users to choose whether they want to exclude the default value or not.By updating the user, we obviously understand that he is active, but in order not to explicitly indicate his status each time, we will simply specify
exclude_unset=Falseand rely on the default value.