1

I'm trying to define a compound key for a custom database table.

My first insert goes through OK but the following inserts fail because the PID value is the same.

What I'm trying to achieve is a state where the uniqueness of each insert is determined by a combination of the PID, QID and UID values of each insertion.

The following is my schema in my install script:

function module_schema() {

  $schema = array();

  $schema['module_records'] = array(
    'description' => '',
    'fields' => array(
      'pid' => array(
        'description' => 'PID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'User ID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'qid' => array(
        'description' => 'QID',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'first' => array(
        'description' => 'First option',
        'type' => 'text',
        'length' => 255,
        'not null' => FALSE,
      ),
      'second' => array(
        'description' => 'Second option',
        'type' => 'text',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array('pid'),
    'indexes' => array(
      'uid' => array('uid'),
      'qid' => array('qid'),
    );

  return $schema;
}

1 Answer 1

0

You may define 'type' => 'serial' if you want to auto increment your primary key.

3
  • There is no auto-incrementing value. All the values are provided
    – sisko
    Jun 1, 2017 at 5:17
  • If all values are given by you then you can create a column <code>id</code> and then increment the id value which will be unique for all rows. Jun 1, 2017 at 5:21
  • Please bare in mind my question is about compound/composite keys. The uniqueness will come from a combination of the 3 keys NOT from a single auto-incrementing key
    – sisko
    Jun 2, 2017 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.