Community Documentation

Primary key fields must be NOT NULL

Last updated October 21, 2012. Created by bjaspan on July 14, 2007.
Edited by kiamlaluno, ax. Log in to edit this page.

SQL databases require that all columns involved in a table's primary key be declared as NOT NULL. If you declare a primary key column to allow NULL values, MySQL and PostgresQL (probably among others) silently convert the columns to NOT NULL, but you should not rely on this behavior.

For example, this table specification is incorrect:

<?php
$schema
['T'] = array(
 
'fields' => array(
   
'col1' => array('type' => 'int')),
 
'primary key' => array('col1'));
?>

To make this specification correct, add 'not null' => TRUE to the specification for col1.

<?php
$schema
['T'] = array(
 
'fields' => array(
   
'col1' => array('type' => 'int', 'not null' => TRUE)),
 
'primary key' => array('col1'));
?>

Comments

a default for the colum used as primary key is not allowed for mysql.

(at least the schema modul reports this)

Thomas

Combining IT and arts to organize
http://it-arts.org

About this page

Drupal version
Drupal 6.x
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here