MySQL Error 1822: Failed to add foreign key constraint; missing index for contraint BUT index exists

后端 未结 2 1288
野趣味
野趣味 2021-01-03 01:55

I am trying to add an foreign key to my flightschedule table but it fails, but I do not really know why. The foreign key should reference the txtAC_tag attribute from the tb

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 02:38

    The issue is here:

    add foreign key(txtAC_tag) references tblaircrafts(txtAC_tag);
    

    here you are binding txtAC_tag to txtAC_tag of tblaircrafts table in a foreign key relationship but in tblaircrafts the column txtAC_tag is neither unique nor primary that's why it is showing error.

    For foreign key relationship, the parent table column on which you are creating relation must be unique or primary and they must have the same datatype and size also.

    To resolve this make txtAC_tag column unique.

提交回复
热议问题