@@ -5618,13 +5618,33 @@ function ( $column ) {
56185618
56195619 if ( 'PRIMARY ' === $ info ['INDEX_NAME ' ] ) {
56205620 if ( $ has_autoincrement ) {
5621- if ( $ has_autoincrement ) {
5622- if ( count ( $ constraint ) > 1 ) {
5623- throw $ this ->new_driver_exception (
5624- 'Cannot combine AUTOINCREMENT and multiple primary keys in SQLite '
5625- );
5626- }
5621+ /*
5622+ * In MySQL, a compound PRIMARY KEY can have an AUTOINCREMENT
5623+ * column, when it is the first column in the key.
5624+ *
5625+ * SQLite doesn't support this, but we can emulate it as follows:
5626+ * 1. Keep only the first column as a PRIMARY KEY.
5627+ * Since this is the column that also has AUTOINCREMENT,
5628+ * it reasonable to assume that its values are unique.
5629+ * 2. Create a UNIQUE key for all the PRIMARY KEY columns.
5630+ * This is to preserve the index of the compound key.
5631+ */
5632+ if ( count ( $ constraint ) > 1 ) {
5633+ $ sqlite_index_name = $ this ->get_sqlite_index_name ( $ table_name , 'primary ' );
5634+ $ create_index_queries [] = sprintf (
5635+ 'CREATE UNIQUE INDEX %s ON %s (%s) ' ,
5636+ self ::RESERVED_PREFIX . $ sqlite_index_name ,
5637+ $ this ->quote_sqlite_identifier ( $ table_name ),
5638+ implode ( ', ' , $ column_list )
5639+ );
56275640 }
5641+
5642+ /*
5643+ * The PRIMARY KEY was already generated with AUTOINCREMENT,
5644+ * as required by SQLite column constraint syntax.
5645+ *
5646+ * @see https://www.sqlite.org/syntax/column-constraint.html
5647+ */
56285648 continue ;
56295649 }
56305650 $ rows [] = sprintf ( ' PRIMARY KEY (%s) ' , implode ( ', ' , $ column_list ) );
0 commit comments