Question

Apologies if this is a newbie question, but I have been unable to resolve this problem for some time and am hoping someone with a better understand of MySQL can assist me in deciphering the following problem:

I have two tables that I want to maintain so that users can define their preferences, while an admin can maintain their access rights to those preferences. And I want to make sure that if an admin removes certain rights, that the preferences table gets updated:

The two tables are:

main_access
+--------------+---------+------+-----+---------+-------+
| Field        | Type    | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| nav_location | int(11) | NO   | PRI | NULL    |       |
| user_id      | int(11) | NO   | PRI | NULL    |       |
+--------------+---------+------+-----+---------+-------+

and

main_prefs
+--------------+---------+------+-----+---------+-------+
| Field        | Type    | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| nav_location | int(11) | NO   | PRI | NULL    |       |
| user_id      | int(11) | NO   | PRI | NULL    |       |
+--------------+---------+------+-----+---------+-------+

They both have unique indexes, as I run this:

CREATE TABLE IF NOT EXISTS main_access (
    nav_location    integer not null,
    user_id         integer not null,
    unique index uk_access (nav_location, user_id)
);

CREATE TABLE IF NOT EXISTS main_pref (
    nav_location    integer not null,
    user_id         integer not null,
    unique index uk_pref (nav_location, user_id)
);

So, uk_access, and uk_pref with the above tables are being created. Yet, when I run:

ALTER TABLE main_pref 
ADD CONSTRAINT FK_access1 
FOREIGN KEY(uk_pref) REFERENCES main_access(uk_access) 
ON UPDATE CASCADE 
ON DELETE CASCADE;

I get the "ERROR 1072 (42000): Key column 'uk_pref' doesn't exist in table" error. Which, to me, makes zero sense to get. I know the key exists, as I also ran the following after creating the tables:

ALTER TABLE main_pref ADD UNIQUE INDEX uk_pref (nav_location, user_id);

and got a duplicate key warning. I did the same thing with the other table.
I also tried making the unique index name the same for both tables. That is, I made them both uk_pref, to no avail. Same error.
I feel like this is a really simple problem that I'm missing, but just haven't been able to see it. I am hoping another set of eyes will point out the mistake I'm making. Any help is much appreciated!

I looked for similar problems, but could not find any on here that addressed the same issue.

EDIT: AJ pointed this out, and I forgot to mention that I tried it:
I also (first) tried bypassing setting unique keys and did the following:

ALTER TABLE main_pref 
ADD CONSTRAINT FK_access1 
FOREIGN KEY(user_id, nav_location) REFERENCES main_access(user_id, nav_location) 
ON UPDATE CASCADE 
ON DELETE CASCADE;

And MySQL just came back a syntax error.

Was it helpful?

Solution

You gave the columns in the key definitions in the wrong order

ALTER TABLE main_pref 
ADD CONSTRAINT FK_access1 
FOREIGN KEY(nav_location,user_id) REFERENCES main_access(nav_location,user_id) 
ON UPDATE CASCADE 
ON DELETE CASCADE;

This works fine

http://sqlfiddle.com/#!2/16751

The FK columns must be given in the same order as in the key definition in the CREATE TABLE statement. Put another way, a composite indes on (nav_location,user_id) cannot be used to satisfy an FK definition on (user_id,nav_location) as in your original post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top