Make users.u_id UNIQUE for use as a FK in user_teams_membership#1587
Merged
Conversation
cpeel
approved these changes
Jun 3, 2026
cpeel
left a comment
Member
There was a problem hiding this comment.
I confirmed on PROD that all values of users.u_id are unique as a sanity check.
cpeel
requested changes
Jun 4, 2026
cpeel
left a comment
Member
There was a problem hiding this comment.
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?
Collaborator
Author
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
approved these changes
Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.phpon an Ubuntu 26.04 VM)