Skip to content

Make users.u_id UNIQUE for use as a FK in user_teams_membership#1587

Merged
cpeel merged 1 commit into
DistributedProofreaders:masterfrom
bpfoley:unique-u-id
Jun 4, 2026
Merged

Make users.u_id UNIQUE for use as a FK in user_teams_membership#1587
cpeel merged 1 commit into
DistributedProofreaders:masterfrom
bpfoley:unique-u-id

Conversation

@bpfoley

@bpfoley bpfoley commented Jun 3, 2026

Copy link
Copy Markdown
Collaborator

MySQL 8.0 InnoDB allows foreign keys to ref an indexed but not unique column.

This is deprecated by default in MySQL 8.4 (in Ubuntu 26.04) and produces the error

Failed to add the foreign key constraint. Missing unique key for constraint 'user_teams_membership_ibfk_1' in the referenced table 'users'

See https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_restrict_fk_on_non_standard_key

(Found when trying to run SETUP/install_db.php on an Ubuntu 26.04 VM)

@cpeel cpeel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirmed on PROD that all values of users.u_id are unique as a sanity check.

@cpeel cpeel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I tried running this on TEST and got the following error. MySQL doesn't like that we're trying to drop the index on an auto column:

cpeel@test:~/u-cpeel-dp/SETUP/upgrade/25 (unique-u-id)$ php 20260603_make_users_u_id_unique.php
Making u_id INDEX in users UNIQUE..

    ALTER TABLE users
        DROP INDEX u_id;

<p class='error'>
Incorrect table definition; there can be only one auto column and it must be defined as a key
</p><pre>
PHP Fatal error:  Uncaught mysqli_sql_exception: Incorrect table definition; there can be only one auto column and it must be defined as a key in /home/cpeel/u-cpeel-dp/SETUP/upgrade/25/20260603_make_users_u_id_unique.php:17
Stack trace:
#0 /home/cpeel/u-cpeel-dp/SETUP/upgrade/25/20260603_make_users_u_id_unique.php(17): mysqli_query()
#1 {main}
  thrown in /home/cpeel/u-cpeel-dp/SETUP/upgrade/25/20260603_make_users_u_id_unique.php on line 17

Wonder how we get out of this pickle?

@bpfoley

bpfoley commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator Author

Actually, I tried running this on TEST and got the following error. MySQL doesn't like that we're trying to drop the index on an auto column:

cpeel@test:~/u-cpeel-dp/SETUP/upgrade/25 (unique-u-id)$ php 20260603_make_users_u_id_unique.php
Making u_id INDEX in users UNIQUE..

    ALTER TABLE users
        DROP INDEX u_id;

<p class='error'>
Incorrect table definition; there can be only one auto column and it must be defined as a key
</p><pre>
PHP Fatal error:  Uncaught mysqli_sql_exception: Incorrect table definition; there can be only one auto column and it must be defined as a key in /home/cpeel/u-cpeel-dp/SETUP/upgrade/25/20260603_make_users_u_id_unique.php:17
Stack trace:
#0 /home/cpeel/u-cpeel-dp/SETUP/upgrade/25/20260603_make_users_u_id_unique.php(17): mysqli_query()
#1 {main}
  thrown in /home/cpeel/u-cpeel-dp/SETUP/upgrade/25/20260603_make_users_u_id_unique.php on line 17

Wonder how we get out of this pickle?

Turns out you can chain a list of 'alter specifications' in a single SQL command...

MySQL 8.0 InnoDB allows foreign keys to ref an indexed but not unique
column.

This is deprecated by default in MySQL 8.4 (in Ubuntu 26.04) and
produces the error

`Failed to add the foreign key constraint. Missing unique key for
constraint 'user_teams_membership_ibfk_1' in the referenced table
'users'`

See https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_restrict_fk_on_non_standard_key
@cpeel cpeel merged commit f26476e into DistributedProofreaders:master Jun 4, 2026
12 checks passed
@bpfoley bpfoley deleted the unique-u-id branch June 4, 2026 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants