mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Support for default values on SQL side or something like that #6454
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @ctxnop on GitHub (Apr 23, 2020).
The issue
I looked for a similar issue as I'm pretty sure I'm not the first one to post that, but couldn't find one... "Default" is a bit too common...
I also now that it's a frequent question: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/faq.html#how-can-i-add-default-values-to-a-column
But it's exactly because of this answer that I feel the urge to post this festure request!
"This is not necessary however, you can just use your class properties as default values." ?!
This is completly and utterly wrong! It's not the same thing at all!
Ok, in some cases it can be used as a workaround, but it's very very limited. But only on some super simple cases.
Default values are way more complex than just a literal value...
Here is a small and easy example using postgresql
So, no, it's not the same thing a setting a default value to the entity's properties...
The whole point of having default values is to benefit from the database settings (which my not use the same datetime/timezone as your code), without having to query data, send them to the client, so that the client can forge a new query to send them back to the database.
The feature
At this point everybody should agree that the documentation linked above is wrong when it says that it's not necessary because defaults values on client side are the same thing.
So, first thing first, the documentation should be edited to present default values on client side as a limited workaround, not as the right solution. It never be and never will be the right solution.
Second, there is another workaround that better answer the question as it is asked: use columnDefinition to write your self the full definition so that you can use the DEFAULT keyword. The downsides of this are that you have to write the full definition your self (other attributes are ignored like type or nullability) and you may loose portability over some databases. But when you ask for default values it's because you known which database will be used. Also most databases that support defaults are using the same syntax so the loss will be limited.
And finally, you really should implement a support for default value, it's an important feature for many people who care about code quality, performances and data reliability.
There is two way of doing so. The first and obvious one is a full support of it. I understand that it can be really complex, slow to implement and requires a lot of testing.
You also can think of it differently: do not support it, just add a "customColumnDefinitionExtra" (pick the name you want) attribute which is added as is at the end of the column definition. So that users can write whatever they want without having to drop all other generated text (types, nullability, etc...)
Document that as a no warranty attribute and you are good to go. I can live with that.