# Name of foreign key in innodb table

**URL:** <https://www.ruby-forum.com/t/name-of-foreign-key-in-innodb-table/107804>\
**Category:** Rails\
**Created:** [June 26, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/name-of-foreign-key-in-innodb-table/107804 "2007-06-26T13:07:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![psimoes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6a8cbe/32.png) [@psimoes](https://www.ruby-forum.com/u/psimoes)\
**Post date:** [June 26, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/name-of-foreign-key-in-innodb-table/107804/1 "2007-06-26T13:07:13Z")

</div>

Hello,

I’m trying to do a ALTER TABLE to add a foreign key. The code is:

execute “ALTER TABLE table1  
ADD CONSTRAINT FOREIGN KEY  
`const_fk_table1_table2_id` (`table2_id`)  
REFERENCES `table2` (`id`)  
ON DELETE CASCADE”

Instead of creating a foreign key constraint with the name  
“const\_fk\_table1\_table2\_id” it will create one with the name  
“table2\_ibfk\_1”.

First I’ve added an index with : add\_index :table1, :table2\_id, :name  
=\> ‘fk\_table1\_table2\_id’

Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![psimoes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/6a8cbe/32.png) [@psimoes](https://www.ruby-forum.com/u/psimoes)\
**Post date:** [June 26, 2007, 1:14pm UTC](https://www.ruby-forum.com/t/name-of-foreign-key-in-innodb-table/107804/2 "2007-06-26T13:14:18Z")

</div>

Problem fixed. The name comes after the CONSTRAINT not the FOREIGN KEY  
keywords.
