Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: error 150 using ON UPDATE SET NULL and ON DELETE SET NULL, why?

i have this:

DROP TABLE IF EXISTS `sf_guard_user`;


CREATE TABLE `sf_guard_user`
(
    `id` INTEGER(11)  NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(128)  NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `sf_guard_user_U_1` (`username`)
)Type=InnoDB;


DROP TABLE IF EXISTS `shop_orders`;

CREATE TABLE `shop_orders`
(
    `orders_id` INTEGER(11)  NOT NULL AUTO_INCREMENT,
    `sfgu_id` INTEGER(11)  NOT NULL,

    PRIMARY KEY (`orders_id`),
    INDEX `shop_orders_FI_1` (`sfgu_id`),
    CONSTRAINT `shop_orders_FK_1`
        FOREIGN KEY (`sfgu_id`)
        REFERENCES `sf_guard_user` (`id`)
        ON UPDATE SET NULL
        ON DELETE SET NULL,

)Type=InnoDB;

and I'm getting this error:

1005 - Can't create table 'prueba1.shop_orders' (errno: 150)

if i do not remove the lines ON UPDATE SET NULL and ON DELETE SET NULL.

Any idea why?

Regards

Javi

like image 245
ziiweb Avatar asked Feb 14 '11 13:02

ziiweb


People also ask

What is error no 150 in mysql?

Altering a table returns an error (errno: 150) if a foreign key definition is incorrectly formed for the altered table. Dropping an index required by a foreign key constraint. The foreign key constraint must be removed before dropping the index.

What does on delete set null mean?

What is a foreign key with "Set NULL on delete" in SQL Server? A foreign key with "set null on delete" means that if a record in the parent table is deleted, then the corresponding records in the child table will have the foreign key fields set to NULL. The records in the child table will not be deleted in SQL Server.

Why my foreign key is null?

A foreign key containing null values cannot match the values of a parent key, since a parent key by definition can have no null values. However, a null foreign key value is always valid, regardless of the value of any of its non-null parts.


1 Answers

I think that is because you declared the field NOT NULL

like image 171
Don Avatar answered Oct 21 '22 08:10

Don