errno: 150 situation

errno: 150 situation

am 11.02.2005 03:22:27 von -

i'm getting an errno: 150 when creating a table and this is what i read
from the mysql manual:

"Both 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."

I only understand the first statement. Can somebody help me decipher
what the other statements mean especially the following:

"where the referenced columns are listed as the first columns in the
same order"

"Index prefixes on foreign key columns are not supported"



i want to create this table

CREATE TABLE tablename
(
from_domain_name ....
from_url_path ...
to_domain_name...
to_url_path

PRIMARY KEY(from_domain_name, from_url_path, to_domain_name, to_url_path),

FOREIGN KEY(from_domain_name)
REFERENCES domain(name)
...
...

FOREIGN KEY(from_url_path)
REFERENCES url(path)
...
...

FOREIGN KEY(to_domain_name)
REFERENCES domain(name)
...
...

FOREIGN KEY(to_url_path)
REFERENCES url(path)
...
...

) ENGINE=InnoDB;

Re: errno: 150 situation

am 11.02.2005 03:51:16 von Bill Karwin

- wrote:
> Can somebody help me decipher
> what the other statements mean especially the following:
>
> "where the referenced columns are listed as the first columns in the
> same order"

If you use a composite key for your reference, the fields must be in the
same order in both the referencing and referenced tables.

For example:

create table master (
a int not null,
b int not null,
c int not null,
primary key (a, b, c)
);

create table dependent (
x int,
y int,
z int,
foreign key (x, y, z) references master(a, b, c)
);

But if you make the foreign key reference anything other than (a,b,c),
for example (b,c,a) or (a,b) or (b,c), it can't check against the
primary key in the referenced table. You must list all the fields
listed in the primary key, and in the same order as they are listed in
that primary key definition.

You can mix up the order of the fields in the foreign key, but then they
might be comparing to the wrong fields in the primary key in the other
table:
foreign key (y, z, x) references master(a, b, c)
In this case, y validates against a, z validates against b, and x
validates against c.

What are your primary key definitions in your other tables url and
domain? Do you have compound keys in those tables, and you're trying to
reference them with single-field foreign keys?

> "Index prefixes on foreign key columns are not supported"

Index prefixes are for when you have very long string fields, and you
want to index the first N characters, to help reduce the overall size of
the index data structure created. You specify the length N of the
prefix when you create the index, for example:

create index foo on myTable (veryLongVarcharField(20));

They're saying you can't use that kind of index in foreign keys, for
whatever reason.

Regards,
Bill K.

Re: errno: 150 situation

am 11.02.2005 13:57:08 von -

Bill Karwin wrote:
> For example:
>
> create table master (
> a int not null,
> b int not null,
> c int not null,
> primary key (a, b, c)
> );
>
> create table dependent (
> x int,
> y int,
> z int,
> foreign key (x, y, z) references master(a, b, c)
> );

somehow i managed to solve the problem by include "INDEX(path)" in the
referenced table. this even works when i removed the TYPE=INNODB
although the manual said it must be an innodb type. will the
constraints work during runtime eventhough it can compile?

Re: errno: 150 situation

am 11.02.2005 19:38:24 von Bill Karwin

- wrote:
> somehow i managed to solve the problem by include "INDEX(path)" in the
> referenced table.

Aha, so the path fields in the other tables weren't indexed? That was
definitely your problem.

> this even works when i removed the TYPE=INNODB
> although the manual said it must be an innodb type. will the
> constraints work during runtime eventhough it can compile?

The constraints will *not* work if you don't make the tables InnoDB.
When you declare a foreign key in a MyISAM table, MySQL accepts the
declaration, but does not enforce the constraint. In other words, you
_are_ permitted to insert values even if they don't occur in the
referenced table.

"In MySQL 3.23.44 and up, InnoDB tables support checking of foreign key
constraints."
http://dev.mysql.com/doc/mysql/en/example-foreign-keys.html

Regards,
Bill K.