Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
It's not possible to create tables with datetime fields using the schema api if you want to have a default value of now() or current_timestamp. This happens because all strings are escaped with ' before going to the database. Database function calls like now() do not work if escaped. The attached patch allows now() to be used for datetime fields.
Issue fork drupal-215821
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
PanchoThis is a bug, as it's torpeding usage of the Schema API and therefore endangering the dbs abstraction.
Comment #2
moshe weitzman CreditAttribution: moshe weitzman commentedwhat about current_timestamp? any others like this?
Comment #3
traxer CreditAttribution: traxer commentedThe behaviour of Drupal's database system does not deviate from what is documented. Also, having the database take care of setting the time by itself is nice to have, but not critical to the usage of a database abstraction layer. So this is a feature request.
The MySQL implementation of the Schema API implements the
datetime
field using MySQL's DATETIME type. That type does not support automatic initializiation/update to the current time.MySQL's type TIMESTAMP supports automatic initializiation/update to the current time. Switching from DATETIME to TIMESTAMP is not an option, though:
See http://dev.mysql.com/doc/refman/4.1/en/timestamp.html for details.
Comment #4
ScoutBaker CreditAttribution: ScoutBaker commentedAs a feature request, I'm moving this to D7.
Comment #5
Crell CreditAttribution: Crell commentedAnd now to D8.
Comment #6
geerlingguy CreditAttribution: geerlingguy commentedAnd now I subscribe.
Comment #7
dozymoe CreditAttribution: dozymoe commented0.0
Comment #8
cosmicdreams CreditAttribution: cosmicdreams commentedrunning into this issue currently. So this appears to be an issue that looking for a solution that tells the DB to properly set a default value when the row is created in the DB.
But I need a solution now. Can I just set the type of the field to datetime and set it's value on node_create?
Comment #9
Damien Tournoud CreditAttribution: Damien Tournoud commented7.x and 8.x do *not* support datetime fields.
Comment #10
BAM5 CreditAttribution: BAM5 commentedDatetime fields are supported, just not as a 'type' type. You have to specify it through the 'mysql_type' and equivalent properties for other database drivers. Besides, who says that datetime fields won't ever be supported in the future making this request completely relevant?
Also, the now() function does not work for inserting values, now() is once again turned into a string quotation.
Comment #11
smartango CreditAttribution: smartango commentedbeing for d8, what about this schema definition:
then in core code:
I put 'on update' because it is present in mysql, how to manage this? any plan? (want do/dont want ..)
- EDIT: is_callable does not really work correctly
- EDIT 2: style
- EDIT 3: I neither look at code, what is ESCAPE?
Comment #12
Crell CreditAttribution: Crell commentedThe schema array is currently serialized, and anonymous functions cannot be serialized.
Comment #15
AaronBaumanShall we call this "won't fix" then?
Comment #20
hkirsman CreditAttribution: hkirsman commented+1 - would be nice to have!
Comment #22
DrupalDope CreditAttribution: DrupalDope commentedI need this too.
Comment #27
Bhanu951 CreditAttribution: Bhanu951 as a volunteer commented