0

I am trying to create an foreign key constraint between two tables but after I execute the alter command mysql creates index instead of foreign key.

I am using hibernate, so initially i thought this to be a problem of hibernate but when i execute the query directly on mysql behavior is same.

alter table person add constraint FK9ircw28d19mdg5pu8yfg1qs8p foreign key (Address_Id) references Address (Address_Id)

After running this command, i get following in mysql database

enter image description here

As you can see constraint is added in indexes rather than as Foreign key.

Hibernate executes following queries while creating tables:

Hibernate: drop table if exists Address

Hibernate: drop table if exists hibernate_sequence

Hibernate: drop table if exists person

Hibernate: create table Address (Address_Id bigint not null, addressLine1 varchar(255), addressLine2 varchar(255), city varchar(255) not null, country varchar(255) not null, state varchar(255) not null, primary key (Address_Id)) engine=MyISAM

Hibernate: create table hibernate_sequence (next_val bigint) engine=MyISAM

Hibernate: insert into hibernate_sequence values ( 1 )

Hibernate: insert into hibernate_sequence values ( 1 )

Hibernate: create table person (Person_Id bigint not null, dob date not null, name varchar(255) not null, Address_Id bigint, primary key (Person_Id)) engine=MyISAM

Hibernate: alter table person add constraint FK9ircw28d19mdg5pu8yfg1qs8p foreign key (Address_Id) references Address (Address_Id)

3
  • What exactly am I looking at? Post the show create table output please. Commented Jul 27, 2018 at 13:05
  • 1
    MyISAM does not support foreign keys. Use InnoDB. Commented Jul 27, 2018 at 13:56
  • Thanks It solved problem. You can put it as answer, i will accept it. Commented Jul 28, 2018 at 7:42

1 Answer 1

0

Thanks @Solarflare. It solved my problem. Issue was MyISAM engine does not support foreign keys. Once I changed the engine type to InnoDB, things worked as expected.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.