db_change_field

Versions
mediamosa-21
db_change_field($table, $field, $field_new, $spec, $keys_new = array())

Changes 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

▾ 30 functions call db_change_field()

block_update_7003 in modules/block/block.install
Change the weight column to normal int.
comment_update_7007 in modules/comment/comment.install
Split {comment}.timestamp into 'created' and 'changed', improve indexing on {comment}.
contact_update_7003 in modules/contact/contact.install
Change the weight column to normal int.
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.
devel_update_5 in sites/all/modules/devel/devel.install
filter_update_7000 in modules/filter/filter.install
Increase the size of {filters}.weight and add {filter_formats}.weight.
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 index and allow longer location.
mediamosa_update_7000 in sites/all/modules/mediamosa/mediamosa.install
Change mediafile metadata STILL_TYPE ENUM value 'SECONDS' to 'SECOND'.
node_update_7000 in modules/node/node.install
Fix node type 'module' attribute to avoid name-space conflicts.
poll_update_7003 in modules/poll/poll.install
Change the weight column to normal int.
profile_update_7002 in modules/profile/profile.install
Change the weight column to normal int.
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_7032 in modules/system/system.install
Alter field hostname to identifier in the {flood} table.
system_update_7037 in modules/system/system.install
Rename action description to label.
system_update_7042 in modules/system/system.install
Upgrade the {url_alias} table and create a cache bin for path aliases.
system_update_7050 in modules/system/system.install
Change {batch}.id column from serial to regular int.
system_update_7051 in modules/system/system.install
make the IP field IPv6 compatible
taxonomy_update_7008 in modules/taxonomy/taxonomy.install
Change the weight columns to normal int.
update_fix_d7_requirements in includes/update.inc
Perform Drupal 6.x to 7.x updates that are required for update.php to function properly.
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.
user_update_7005 in modules/user/user.install
Change the users table to allow longer email addresses - 254 characters instead of 64.
_mediamosa_migration_mediafile_metadata in sites/all/modules/mediamosa_migration/mediamosa_migration.inc
Migrate the mediafile metadata table.

Code

includes/database/database.inc, line 2778

<?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);
}
?>