When attempting to add a not null field with no default value but specifying an initial value the following error is encountered.

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0='ip', 1='0.0.0.0'' at line 1: UPDATE {node_revision} SET 0=:db_update_placeholder_0, 1=:db_update_placeholder_1; Array
(
[:db_update_placeholder_0] => ip
[:db_update_placeholder_1] => 0.0.0.0
)
in db_add_field() (line 2719 of includes/database/database.inc).

Field definition items:

$schema['node_revision']['fields']['ip'] = array(
  'description' => 'The IP address from which the revision was made',
  'type' => 'varchar',
  'length' => 23, // allow for IPv6 addresses
  'not null' => TRUE,
  'initial' => '0.0.0.0',
);

add field call: db_add_field('node_revision', 'ip', $schema['node_revision']['fields']['ip']);

I believe this is due to line 340 of includes/database/mysql/schema.inc being this

->fields(array($field, $spec['initial']))

instead of

->fields(array($field => $spec['initial']))

I'll submit a patch momentarily.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tekante’s picture

Crell’s picture

Status: Active » Needs review

Looks right to me, but we'll let the testbot have a crack at it. Is there any chance of getting a unit test here, too?

haffmans’s picture

Version: 7.0-beta2 » 7.0-rc1
Status: Needs review » Reviewed & tested by the community

Ran into the same problem in RC1. I need the functionality for a module I'm updating for D7.

The patch in #1 works for me and solves the problem.

Damien Tournoud’s picture

Version: 7.0-rc1 » 7.x-dev
Status: Reviewed & tested by the community » Needs review
FileSize
6.98 KB

Introducing full coverage of addition and removal of standard columns.

Crell’s picture

Status: Needs review » Reviewed & tested by the community

Pretty! Thanks, Damien. (I didn't review the unit tests line by line, but Damien's writing it and they clearly pass, so I'm OK with that.)

webchick’s picture

Status: Reviewed & tested by the community » Fixed

A one line bug fix and 100+ lines of tests. Now that's a patch! :D

Committed to HEAD. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.