Download & Extend

Impossible to create datetime fields with default value of 'now()' using schema-api

Project:Drupal core
Version:6.x-dev
Component:database system
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs work

Issue Summary

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.

AttachmentSizeStatusTest resultOperations
patch.txt2.73 KBIgnored: Check issue status.NoneNone

Comments

#1

Category:feature request» bug report

This is a bug, as it's torpeding usage of the Schema API and therefore endangering the dbs abstraction.

#2

what about current_timestamp? any others like this?

#3

Category:bug report» feature request
Status:needs review» needs work

The 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:

  • TIMESTAMP behaves differently in MySQL versions 4.1, 4.1.2, 4.1.3, 4.1.6
  • The first TIMESTAMP column in a table behaves different than other TIMESTAMP columns in that table.

See http://dev.mysql.com/doc/refman/4.1/en/timestamp.html for details.

#4

Version:6.x-dev» 7.x-dev

As a feature request, I'm moving this to D7.

#5

Version:7.x-dev» 8.x-dev

And now to D8.

#6

And now I subscribe.

#7

0.0

#8

running 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?

#9

Version:8.x-dev» 6.x-dev

7.x and 8.x do *not* support datetime fields.

nobody click here