fosstodon.org is one of the many independent Mastodon servers you can use to participate in the fediverse.
Fosstodon is an invite only Mastodon instance that is open to those who are interested in technology; particularly free & open source software. If you wish to join, contact us for an invite.

Administered by:

Server stats:

10K
active users

Question for the #MySQL / #MariaDB buffs, I have three tables defined thus:

CREATE TABLE IF NOT EXISTS `user` (
	`uid` mediumint unsigned NOT NULL auto_increment COMMENT 'sequential ID',
	...
	 PRIMARY KEY(`uid`)
);
CREATE TABLE IF NOT EXISTS `gserver` (
	`id` int unsigned NOT NULL auto_increment COMMENT 'sequential ID',
	...
	 PRIMARY KEY(`id`)
);

CREATE TABLE IF NOT EXISTS `user-gserver` (
	`uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'Owner User id',
	`gsid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'Gserver id',
	...
	PRIMARY KEY(`uid`,`gsid`),
	FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE,
	FOREIGN KEY (`gsid`) REFERENCES `gserver` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
);

Running the last query triggers the error Foreign key constraint is incorrectly formed. Is there no way to reference multiple tables in foreign keys out of a compound primary key?

@hypolite It's because gserver id is defined as 'int unsigned' whereas in user-gserver the corresponding column is defined as 'mediumint unsigned'. So there is a data type mismatch.

@dbdemon Changing the `user-gserver`.`gsid` field type to int unsigned enabled the creation of the table. 😌
Karl Levik Ⓥ

@hypolite
Glad I could help! MySQL and MariaDB are super-strict on this. In your case it makes sense (although the error message could be more helpful!), but they will also refuse to accept a mismatch on things that shouldn't matter, e.g. int(10) vs int(11) - where the numbers in parentheses only matter for display/formatting purposes inside the mysql and mariadb command-line clients.