I'm trying to add a foreign key constraint to my existing MYSQL database but I can't seem to get it working.
Working with Mysql:
+-----------+
| VERSION() |
+-----------+
| 8.0.32 |
+-----------+
Table 1:
+------------------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+------------------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+
| author_id | mediumint | NULL | NO | MUL | 0 | | select,insert,update,references | |
Table 2:
+---------------------------+-------------------+--------------------+------+-----+---------+----------------+---------------------------------+-----------------------------------------------------------------------------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+---------------------------+-------------------+--------------------+------+-----+---------+----------------+---------------------------------+-----------------------------------------------------------------------------+
| member_id | mediumint | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
The command:
ALTER TABLE `forums_posts` ADD CONSTRAINT `forums_posts_author_id_fkey` FOREIGN KEY (`author_id`) REFERENCES `core_members`(`member_id`) ON DELETE RESTRICT ON UPDATE CASCADE;
I'm using the INFORMATION_SCHEMA.TABLE_CONSTRAINTS
table to check whether it exists. Before running the command:
mysql> select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE = 'FOREIGN KEY';
Empty set (0.00 sec)
After running the command:
mysql> ALTER TABLE `forums_posts` ADD CONSTRAINT `forums_posts_author_id_fkey` FOREIGN KEY (`author_id`) REFERENCES `core_members`(`member_id`) ON DELETE RESTRICT ON UPDATE CASCADE;
Query OK, 164286 rows affected (3.24 sec)
Records: 164286 Duplicates: 0 Warnings: 0
mysql> select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE = 'FOREIGN KEY';
Empty set (0.00 sec)
Things I've tried:
- Checked Collation is the same - they're both set to NULL so I'm assuming that means they're the same
- Checked the type is the same - both are mediumint
I was initially doing this through the Prisma
ORM, but once I noticed that the migration wasn't working I tried to dig deeper into it. Now I'm trying to run these commands manually but the issue seems the same.
Command seems to run, but the changes don't exist anywhere. This causes issues with the ORM as its schema and the db schema have drifted.
Any ideas?
EDIT:
Engine for forums_posts
is:
mysql> SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='forums_posts';
+--------+
| ENGINE |
+--------+
| MyISAM |
+--------+