drupalprimary-keydrupal-schema

Multiple primary keys in a schema


I have this schema which I need to define two primary keys; one is Drupal's 'vid' field and the other one is my site 'bid' field which is of auto increment type which in turn requires it to be a primary key: wtherwise I get MySQL error. I'm having trouble finding syntax for defining multiple primary keys in a Drupal schema. If anyone can help me out with the syntax, I pretty much appreciate it.

$schema['rft'] = array(
    'fields' => array(
        'vid' => array(
            'type' => 'int',
            'unsigned' => TRUE,
            'not null' => TRUE,
            'default' => 0,
        ),
        'nid' => array(
            'type' => 'int',
            'unsigned' => TRUE,
            'not null' => TRUE,
            'default' => 0,
        ),
        'bid' => array(
            'type' => 'serial',
            'size' => 'medium',
            'not null' => TRUE,                         
        ),

    ),
    'indexes' => array(
        'nid' => array('nid'),
    ),
    'primary key' => array('vid'),  //array('vid','bid') doesn't work
);

return $schema;
}

Solution

  • Using the following worked just fine for me. Maybe it's a MySQL version-specific limitation? Can you report back the actual error message you got when Drupal tried to create this table?

    $schema['rft'] = array(
        'fields' => array(
            'vid' => array(
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
            ),
            'nid' => array(
                'type' => 'int',
                'unsigned' => TRUE,
                'not null' => TRUE,
                'default' => 0,
            ),
            'bid' => array(
                'type' => 'serial',
                'size' => 'medium',
                'not null' => TRUE,                         
            ),
    
        ),
        'indexes' => array(
            'nid' => array('nid'),
        ),
        'primary key' => array('vid', 'bid'),
    );