7 ms·
They could also have done default=uuid.uuid4 to have a new id each time, or default=lambda : str(uuid.uuid4()). It's not really related to whether or not it's a
by Lunrtick 2y ago
They could also have done default=uuid.uuid4 to have a new id each time, or default=lambda : str(uuid.uuid4()). It's not really related to whether or not it's a database default.
It seems quite unfair to place the blame on SQLAlchemy here, or even Python.
Even a statically typed language wouldn't prevent this kind of issue - the author of the code is the only person who can decide when they mean "use this exact string each time" or "use this function to give me a new string each time".
I suppose a column description API could follow the dataclass style definition, with different argument names for default and default_func. That would (I think) prevent this from happening.
- jowea 2y agoI could imagine some sort of static analysis saying that using a constant value instead of a function is a mistake for columns with unique set.
- throw156754228 2y agoExactly, this is an error that should've been picked up statically, before unit even.
- Lunrtick 2y agoThat's a good point! I suppose in Python that could be a guard in the column definition function.