- 🇦🇺Australia taggartj
This is still an issue in drupal 10 please see
/** * Adds a prime key id so I can finish early today. */ function MYMODULE_update_8002() { // Add a prime key for views base. $spec = [ 'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, ]; $db = \Drupal::database(); $schema = $db->schema(); if ($schema->fieldExists('some_custom_table', 'id')) { $schema->dropField('some_custom_table', 'id'); } // THESE HERE Don't work as they should //$schema->addField('some_custom_table', 'id', $spec, ['id']); //$schema->addPrimaryKey('some_custom_table', ['id']); // Resulting to hackery. $table = 'some_custom_table'; $col = 'id'; $db->query('ALTER TABLE {' . $table . '} ADD {'. $col .'} INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY ({'. $col .'})'); }