BigJobbies
BigJobbies

Reputation: 4055

PHP / MySQL - Foreign Key Issue

Im following a tutorial at the moment and learning how to build something into my existing site.

The tutorial tells me that i need to run this against my database

ALTER TABLE posts ADD FOREIGN KEY(post_by) REFERENCES users(user_id) ON DELETE RESTRICT ON UPDATE CASCADE;

and im receiving an error saying it cant create table.

the 'users' table is from my existing database and everything else is new.

The tutorial gives me a few alter commands to run and they all go perfect, except when im trying to use the 'users' table.

Im totally stuck on this, any help would be greatly appreciated.

Cheers

Upvotes: 3

Views: 266

Answers (2)

SaidbakR
SaidbakR

Reputation: 13554

Check your table type. It should be InnoDB.

Upvotes: 8

Memochipan
Memochipan

Reputation: 3465

Take a look to http://dev.mysql.com/doc/refman/5.1/en/alter-table.html, there is very clear what options are available to each engine.

Take a look to the section that describes:

The FOREIGN KEY and REFERENCES clauses are supported by the InnoDB storage engine

And:

For other storage engines, the clauses are parsed but ignored.

And as h2ooooooo said you can change your engine with:

ALTER TABLE table ENGINE = INNODB

Upvotes: 1

Related Questions