Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Applying 2020042101.sql to existing MariaDB causes error 150 "Foreign key constraint is incorrectly formed" #2783

Closed
egoexpress opened this issue Jun 23, 2020 · 4 comments

Comments

@egoexpress
Copy link
Contributor

While trying to do this:

During an update I tried to apply https://github.com/idno/known/blob/master/warmup/schemas/mysql/2020042101.sql to an existing MariaDB Known database. It failed to create all 3 tables contained in the file with the error above.

I encountered this error on the MariaDB side:

Can't create table 'entities_metadata' (errno: 150 "Foreign key constraint is incorrectly formed")

I found the following solution:

It seems this error is caused if the column a foreign key references to doesn't exactly match the foreign key column itself.

I digged around a bit and it seems the collate setting for entities._id and entities_metadata._id don't match (at least in my case).
I was able to resolve the issue by adding collate 'utf8_unicode_ci' to the _id column in the CREATE statement.s Adding this option lets me create the 3 tables.

Example for entities_metadata:

CREATE TABLE IF NOT EXISTS `entities_metadata` (
  `_id` varchar(32) NOT NULL collate 'utf8_unicode_ci',
  `name` varchar(64) NOT NULL,
  `value` text NOT NULL,
  KEY `value` (`value`(255)),
  KEY `name` (`name`),
  KEY (`_id`),
  CONSTRAINT `em_id_id` FOREIGN KEY (`_id`) REFERENCES `entities` (`_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Some other notes:

I'm using MariaDB as noted above.
I ran an older GitHub release of Known and tried to update to the latest GitHub master (1.2.3+2020061901).

@mapkyca
Copy link
Member

mapkyca commented Jun 24, 2020

Interesting... I've not seen this when I updated things, but maybe that was related to the default configured... Perhaps this is the sort of thing we need to define...

@egoexpress
Copy link
Contributor Author

The first post on my site is from late 2015, so the initial DB was set up a while ago. :)
Maybe it's an option to define collate at least for the key columns in the default install so these errors won't occur in the future.

I just wanted to raise this issue here so that anyone with similar problems has a starting point to get his Known instance up and running again.

@mapkyca
Copy link
Member

mapkyca commented Jun 28, 2020

Yeah, thanks for that.

I really want to move the *sql over to a schema manager based around doctrine dbal, but quite honestly I've not had time. Managing SQL in .sql files is not a good way of doing things in $currentYear

@benwerd
Copy link
Member

benwerd commented Dec 24, 2020

Closing out in favor of the larger issue, which is #2859.

@benwerd benwerd closed this as completed Dec 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants