Forum Moderators: open

Message Too Old, No Replies

FOREIGN KEY problem in MySQL

         

dbarasuk

10:58 am on Jun 10, 2009 (gmt 0)

10+ Year Member



Hello,
I am getting 1005 error: Can't create table... (errorno 150).

THis is probably being caused by a problem of foreign key bad definition.

The table is defined as follows:

DROP TABLE IF EXISTS presences;
CREATE TABLE IF NOT EXISTS presences(

etuid int not null,
dateid int not null,

am char(1) not null DEFAULT "U",
pm char(1) not null default "U",
primary key(etuid,dateid),
index(etuid,dateid),
foreign key (etuid) references etudiant (etuid) on update cascade on delete cascade ,
foreign key (dateid) references datecours (dateid) on update cascade on delete cascade
)ENGINE=InnoDB;

The referenced columns are defined as INT in the referenced tables

I just get the message saying that the table "presences" cannot be created. Who can detect what is the problem?

Thanks

Demaestro

4:18 pm on Jun 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If a table you are referencing is a myISAM table type then FK creation won't work

Change the table types for all tables to InnoDB.

Foreign keys definitions are subject to the following conditions:

- All tables must be InnoDB type.

- In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order.

- In the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.

- Index prefixes on foreign key columns are not supported. One consequence of this is that BLOB and TEXT columns cannot be included in a foreign key, because indexes on those columns must always include a prefix length.