0

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:

  1. Checked Collation is the same - they're both set to NULL so I'm assuming that means they're the same
  2. 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 |
+--------+
lpetrucci
  • 1,285
  • 4
  • 22
  • 40
  • What is the storage engine for `forums_posts`? `SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='forums_posts';` – Bill Karwin Jun 11 '23 at 14:38
  • It's `MyISAM`. Added to the OP. – lpetrucci Jun 11 '23 at 14:45
  • I recommend never use MyISAM. In addition to the fact that it doesn't support foreign keys, see [my answer to MyISAM vs. InnoDB](https://stackoverflow.com/questions/20148/myisam-versus-innodb/17706717#17706717) for a description of why to avoid MyISAM. – Bill Karwin Jun 11 '23 at 14:49
  • I had no idea. This is a db generated by an external forum software so i didn't pick one specifically. Thank you – lpetrucci Jun 11 '23 at 14:56

0 Answers0