简体   繁体   中英

MySQL InnoDB table adding foreign key errno:150

Well, i have two tables

publica_evento_grupo

Field | Type | Null | Key | Default | Extra
id_evento_grupo int(10) unsigned    NO  PRI     auto_increment
id_evento       int(8)  unsigned    NO  MUL     
id_grupo        int(8)  unsigned    NO  MUL     
identificacao   varchar(55)         NO  

publica_identificacao_publicacao

Field | Type | Null | Key | Default | Extra
id_evento       int(8) unsigned NO  PRI     
identificacao   varchar(55)     NO  PRI     

publica_evento_grupo . id_evento in is a foreign key to a third table called publica_evento but is also a foreign key together with the column to the table publica_identificacao_publicacao . identificacao . The problem is, I got to create the foreign key that relates publica_evento_grupo to publica_identificacao_publicacao by the key id_evento , but when I try to create the another FK with the column identificacao , it gives the errno below

 [Err] 1005 - Can't create table '#sql-1049_1980992' (errno: 150).

As you can see the table publica_identificacao_publicacao has two PK and that iss the reason that it has to be 2 FK to relate them, i didn't create the indexes yet, because as far as i know, i just have to create the indexes after adding the FK contraints, and I was able to create the FK contraint to the column id_evento between evento_grupo and identificacao_publicacao , i don't know why just the column identificacao is giving this error

EDIT 1: @RolandBouman i don't have the permission to use that command SHOW ENGINE INNODB STATUS

EDIT 2: @Geoff_Montee acctually the command you passed worked, to undestand why i use that structure take a look at this question MySQL UNIQUE Constraint multiple columns condition

Without the actuall DDL it's not so easy to say. I recommend doing:

SHOW ENGINE INNODB STATUS;

Immediately after you run into this error. In the output, look for a section that looks like:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
121026 22:40:18 Error in foreign key constraint of table test/#sql-154c_94:
foreign key(rid) references bla(id):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

You'll find detailed info there about what's wrong with your foreign key definition.

That error usually happens because there is a type mismatch between the referring table and the referred table. In this case, the types seem to match.

Have you considered having a constraint for both fields in the primary composite key? You might have to remove the existing foreign key constraint on id_evento first.

ALTER TABLE publica_evento_grupo ADD FOREIGN KEY 
    (id_evento, identificacao) REFERENCES 
    publica_identificacao_publicacao (id_evento, identificacao);

It seems as though the error in this case might be because you try to add a foreign key constraint to only of a primary key. 主键的

Does identificacao exist in any other tables? Why do you want to add a foreign key constraint to only part of a composite primary key?

Think about it this way...

Let's say you do have a foreign key constraint in the publica_evento_grupo table to the identificacao field by itself. Let's also say in the publica_identificacao_publicacao table you have (1, "some string") and (2, "some string") . So if you delete (2, "some string") , but leave (1, "some string") in its place. The rows that refer to (1, "some string") in the publica_evento_grupo table will complain, even though they shouldn't!

So it sounds to me like you should add a foreign key constraint to all of a primary key or none of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM