sultan
sultan

Reputation: 6058

Drupal: module update

I've module and I've update to alter database table, shortly I need to do something like

ALTER TABLE `TABLE` ADD `FIELD` INT UNSIGNED NOT NULL AFTER `SOME_FIELD` 

so is there any built in function in Drupal to make this changes I considered db_add_field function didn't work?

Sultan

Upvotes: 0

Views: 531

Answers (2)

dastanko
dastanko

Reputation: 121

Using db_add_field()

db_add_field('TABLE', 'FIELD', "VARCHAR( 255 ) NOT NULL DEFAULT '0' AFTER FIELD_2");

Upvotes: 2

nicholas.alipaz
nicholas.alipaz

Reputation: 505

The above does not work, for one it leaves out the first argument (the reference to $ret) and the fourth argument will not allow a raw sql query, only a structured array.

What I had to do was this (change hook_update_N to modulename_update_XXXX as per the drupal api documentation of course):

function hook_update_N(&$sandbox) {
  // We use update_sql here, instead of db_add_field because we cannot specify
  // AFTER in the db_add_field.
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {table} ADD `FIELD` INT UNSIGNED NOT NULL AFTER `SOME_FIELD`");
  return $ret;
}

Hope this helps someone else.

Upvotes: 0

Related Questions