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.
In D6 I did it like this:
function server_install() {
drupal_install_schema('server');
db_query('ALTER TABLE {server} ADD `timestamp` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP');
db_query('ALTER TABLE {server} ADD INDEX ( `timestamp` )');
}
From the D7 documentation it looks like you could do it something like below but I haven't yet been able to figure out the missing bits. Note that I took out a few fields etc. from below so that only what's needed is there for the question.
function server_schema() {
$schema['server'] = array(
'description' => '...',
'fields' => array(
'user_id' => array(
'...' => '...',
'...' => '...',
),
'timestamp' => array(
'mysql_type' => 'timestamp',
'description' => 'Timestamp field',
'not null' => TRUE,
),
),
'primary key' => array('user_id', 'type'),
'indexes' => array(
'timestamp' => array('timestamp'),
),
);
}
Some help much appreciated!
Comments
Comment #1
Reg CreditAttribution: Reg commentedJust found out that things are being done in Drupal 8.x and then getting back ported now.
Comment #2
joachim CreditAttribution: joachim commentedI'm not sure that's supported by the schema API: http://drupal.org/node/159605
Comment #3
Reg CreditAttribution: Reg commentedHow would I put a suggestion or request in to make it supported?
Comment #4
joachim CreditAttribution: joachim commentedLike this I guess :)
Comment #5
Reg CreditAttribution: Reg commentedOkay, I'm laughing now, thanks !
Comment #8
AaronBaumanlooks like same as #215821: Impossible to create datetime fields with default value of 'now()' using schema-api
Comment #9
Zac_JH CreditAttribution: Zac_JH commentedNot sure if useful or not, but I solve the problem by using the hook_install: (there is some specific code for dealing with storing original creation date and updating updated dates)
NOTE: Updating the column fails, but removing and adding works fine and keeps the schema infomation correct
Comment #10
Asirde CreditAttribution: Asirde commentedthanks man. this just save me.