The SQLite driver doesn't respond well to compound primary keys, if one of the columns is a serial. Example from a module:

    'fields' => array(
      'id'  => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Entity id',
      ),
      'path'  => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'The path of the page to apply meta tags to',
      ),
      'lang'  => array(
        'type' => 'varchar',
        'length' => 8,
        'not null' => TRUE,
        'description' => 'Language code',
      ),
    ),
  'primary key' => array('id', 'path', 'lang'),

While the usefulness of the above primary key is up for discussion, it is valid, and will work fine in MySOL. However, when SQLite generates the CREATE TABLE, it adds PRIMARY KEY to the field definition in createFieldSql, and removes it from the set. After that, if the primary key set is not empty, it adds a PRIMARY KEY definition to the CREATE TABLE, using the left over columns, causing SQLite to complain 'PDOException: SQLSTATE[HY000]: General error: 1 table "metatags_quick_path_based" has more than one primary key'.

While it's possible to work around, it does break the consistency of the Database API.

I discovered this problem with metatags_quick 2.4, but a quick search tells me that other modules have run into the same bug.

CommentFileSizeAuthor
#5 1571842_workaround.patch787 bytesdpovshed
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dpovshed’s picture

Confirmed - the problem exists.

The metatags_quick is a good example to reproduce and debug.

Damien Tournoud’s picture

Status: Active » Closed (won't fix)

This table definition doesn't make any sense.

Compound primary keys containing a serial columns have no purpose, because the serial part is already unique. I suggest you fix the definition of the table.

Xen’s picture

Status: Closed (won't fix) » Active

I know it doesn't make any sense, the issue is that its a non-sensiality that's not uncommon in contrib.

The problem is that it works fine on MySQL, which means that those not knowing better doesn't find out for a long time.

If we define it as non-sensial, and wont support it for SQLite, why do we implicitly support it for MySQL/Postgres? If it's a common error, shouldn't the abstraction layer catch this and throw an error regardless of the database driver? That's exactly the reason for the existence of the database API, to provide a uniform interface, regardless of differences and bugs in the underlying systems.

Damien Tournoud’s picture

Title: SQLite, serials and primary key » Check the database schemas for common issues to improve portability
Version: 7.14 » 8.x-dev
Category: bug » feature

We don't do any error checking in the schema API right now. I'm not saying we shouldn't be, it is just the way it is right now. For Drupal 7, you would be better off opening issues in the affected modules to raise awareness among the module maintainers.

Let's move to Drupal 8 for discussion on how to improve the situation.

dpovshed’s picture

Title: Check the database schemas for common issues to improve portability » SQLite, serials and primary key
Version: 8.x-dev » 7.14
Category: feature » bug
Status: Active » Closed (won't fix)
FileSize
787 bytes

@Xen, in case you're (or someone else) not in the mood to patch all the modules with suspicious definitions attached is the workaround patch.

It fix definition on the fly and allows all of such modules to be installed correctly.

dpovshed’s picture

Title: SQLite, serials and primary key » Check the database schemas for common issues to improve portability
Version: 7.14 » 8.x-dev
Category: bug » feature
Status: Closed (won't fix) » Active

Sorry guys, restoring headers

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Active » Postponed (maintainer needs more info)

Wonder if this is still needed for D10?

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.