cancel
Showing results for 
Search instead for 
Did you mean: 

Create Foreign Key on customer table

SOLVED

Create Foreign Key on customer table

Hi,

 

I'm trying to add a foreign key on the customer_entity table, but it doesn't work:

 

CREATE TABLE IF NOT EXISTS test (
	id INT(10) NOT NULL,
	customer_id INT(10) NOT NULL,
	PRIMARY KEY (id),
	KEY (customer_id),
	INDEX ix_customer_id (customer_id ASC),
	CONSTRAINT fk_customer_id FOREIGN KEY (customer_id)
		REFERENCES customer_entity(entity_id)
		ON DELETE CASCADE
		ON UPDATE CASCADE
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8

Here's the error message:

QLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

 

What is wrong?

 

Thanks.

 

Regards

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Create Foreign Key on customer table

Hello,

 

I think the reason for this issue is that foreign key customer_id isn't correct. Because customer_entity(entity_id) is unsigned not null. So, we need to change it to:

customer_id INT(10) UNSIGNED NOT NULL,

If still not working, please add SET FOREIGN_KEY_CHECKS=0; at the top of our sql. 

Problem solved? Click Accept as Solution!

View solution in original post

2 REPLIES 2

Re: Create Foreign Key on customer table

Hello,

 

I think the reason for this issue is that foreign key customer_id isn't correct. Because customer_entity(entity_id) is unsigned not null. So, we need to change it to:

customer_id INT(10) UNSIGNED NOT NULL,

If still not working, please add SET FOREIGN_KEY_CHECKS=0; at the top of our sql. 

Problem solved? Click Accept as Solution!

Re: Create Foreign Key on customer table

The solution was the missing UNSIGNED key word.

 

Thanks for you help.