db_change_field

Versions
6
db_change_field(&$ret, $table, $field, $field_new, $spec, $new_keys = array())
7
db_change_field($table, $field, $field_new, $spec, $keys_new = array())

Change a field definition.

IMPORTANT NOTE: To maintain database portability, you have to explicitly recreate all indices and primary keys that are using the changed field.

That means that you have to drop all affected keys and indexes with db_drop_{primary_key,unique_key,index}() before calling db_change_field(). To recreate the keys and indices, pass the key definitions as the optional $keys_new argument directly to db_change_field().

For example, suppose you have:

<?php

$schema['foo'] = array(
'fields' => array(
'bar' => array('type' => 'int', 'not null' => TRUE)
),
'primary key' => array('bar')
);

?>

and you want to change foo.bar to be type serial, leaving it as the primary key. The correct sequence is:

<?php

db_drop_primary_key('foo');
db_change_field('foo', 'bar', 'bar',
array('type' => 'serial', 'not null' => TRUE),
array('primary key' => array('bar')));

?>

The reasons for this are due to the different database engines:

On PostgreSQL, changing a field definition involves adding a new field and dropping an old one which* causes any indices, primary keys and sequences (from serial-type fields) that use the changed field to be dropped.

On MySQL, all type 'serial' fields must be part of at least one key or index as soon as they are created. You cannot use db_add_{primary_key,unique_key,index}() for this purpose because the ALTER TABLE command will fail to add the column without a key or index specification. The solution is to use the optional $keys_new argument to create the key or index at the same time as field.

You could use db_add_{primary_key,unique_key,index}() in all cases unless you are converting a field to be type serial. You can use the $keys_new argument in all cases.

Parameters

$table Name of the table.

$field Name of the field to change.

$field_new New name for the field (set to the same as $field if you don't want to change the name).

$spec The field specification for the new field.

$keys_new Optional keys and indexes specification to be created on the table along with changing the field. The format is the same as a table specification but without the 'fields' element.

Related topics

▾ 17 functions call db_change_field()

comment_update_7007 in modules/comment/comment.install
Split {comment}.timestamp into {comment}.created and {comment}.changed.
dblog_update_6000 in modules/dblog/dblog.install
Allow longer referrers.
dblog_update_7001 in modules/dblog/dblog.install
Allow NULL values for links.
dblog_update_7003 in modules/dblog/dblog.install
Allow longer type values.
filter_update_7003 in modules/filter/filter.install
Remove hardcoded numeric deltas from all filters in core.
locale_update_7000 in modules/locale/locale.install
Add context field and allow longer location.
statistics_update_6000 in modules/statistics/statistics.install
Allow longer referrers.
system_update_6048 in modules/system/system.install
Increase the size of the 'load_functions' and 'to_arg_functions' fields in table 'menu_router'.
system_update_7008 in modules/system/system.install
Use the poll_choice primary key to record votes in poll_votes rather than the choice order. Rename chorder to weight.
system_update_7018 in modules/system/system.install
Shorten the {system}.type column and modify indexes.
system_update_7026 in modules/system/system.install
Increase permitted length of url aliases to 255 characters.
system_update_7032 in modules/system/system.install
Alter field hostname to identifier in the {flood} table.
system_update_7038 in modules/system/system.install
Rename action description to label.
system_update_7042 in modules/system/system.install
Rename dst and src to source and alias.
user_update_7000 in modules/user/user.install
Increase the length of the password field to accommodate better hashes.
user_update_7002 in modules/user/user.install
Convert user time zones from time zone offsets to time zone names.
user_update_7004 in modules/user/user.install
Add the user's pictures to the {file} table and make them managed files.

Code

includes/database/database.inc, line 2577

<?php
function db_change_field($table, $field, $field_new, $spec, $keys_new = array()) {
  return Database::getConnection()->schema()->changeField($table, $field, $field_new, $spec, $keys_new);
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.