For some context, I am creating a javascript bot for discord and I'm creating a large database so that I can log many different pieces of information.
This part of the database is used so that I can
- store messages
- store historical messages, with every edit
- easily look up which users are mentioned within these historical messages.
I am using MariaDB with MySQL Workbench under arch linux. Here's the relevant part of the database design, done on dbdesigner.net. The problem happens at the left-most table.
And here's my error message:
Finished executing script ERROR 1005 (HY000) at line 158: Can't create table
test
.#sql-1bb_6
(errno: 150 "Foreign key constraint is incorrectly formed") Operation failed with exitcode 1
So I know that there's a problem with my foreign key at like 158 but I can't find the problem.
ALTER TABLE `messageContentHistoryMentions`
ADD CONSTRAINT `messageContentHistoryMentions_fk1`
FOREIGN KEY (`messageContentCount`) REFERENCES `messageContentHistory`(`messageContentCount`);
The part that puzzles me about this is that 158 is formatted in the exact same way as line 156, only with different fields. Line 156 below runs:
ALTER TABLE `messageContentHistoryMentions`
ADD CONSTRAINT `messageContentHistoryMentions_fk0`
FOREIGN KEY (`messageID`) REFERENCES `messageContentHistory`(`messageID`);
You can probably understand why I am very confused.
Some more information:
- This is a new install of MariaDB so assume defaults
- InnoDB is the engine being used
- The tables all have a collation of
utf8mb4_unicode_ci
- Both sides of the foreign key are primary keys, are integer and have a length of 8, default of 0.
Thanks.
edit: this is not a duplicate as while the error is similar, the situation it is in is very different
messageID
is already an index, such as a primary key, and the other field is not. So, make sure thatmessageContentCount
is indexed for themessageContentHistory
table. – Paul T.