Description:
I am having problem with constraints for ndb. I am porting an appication from innodb to ndb. Few of the constraints don't get added on ndb. I have noticed that constraint to a column that’s part of a composite primary key index doesn't work on ndb.
Following example for innodb works very well.
mysql> CREATE TABLE `new_products` (
-> `id` int(10) unsigned NOT NULL,
-> `extension_id` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `name` varchar(255) COLLATE latin1_general_ci NOT NULL,
-> `min_months` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `max_months` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `default_months` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `min_years` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `max_years` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `default_years` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `desctype` int(10) unsigned NOT NULL,
-> `type` enum(‘new’,'renewal’,'refund’) COLLATE latin1_general_ci DEFAULT NULL,
-> `renewal_service` varchar(25) COLLATE latin1_general_ci DEFAULT NULL,
-> `product_group_id` int(10) unsigned DEFAULT NULL,
-> `navision_code` varchar(32) COLLATE latin1_general_ci DEFAULT ”,
-> `pretty_name` varchar(255) COLLATE latin1_general_ci DEFAULT ”,
-> PRIMARY KEY (`id`,`extension_id`),
-> KEY `name` (`name`),
-> KEY `type` (`type`),
-> KEY `desctype` (`desctype`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
-> ;
Query OK, 0 rows affected (0.04 sec)
mysql> CREATE TABLE `customer_referral_commission` (
-> `product_id` int(10) unsigned NOT NULL,
-> `extension_id` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `customer_commission` decimal(9,2) unsigned DEFAULT ’0.00′,
-> `date_entered` datetime DEFAULT NULL,
-> `last_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-> PRIMARY KEY (`product_id`,`extension_id`),
-> KEY `product_id` (`product_id`),
-> KEY `extension_id` (`extension_id`),
-> CONSTRAINT `customer_referral_commission_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `new_products` (`id`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
Query OK, 0 rows affected (0.02 sec)
Following for ndb doesnt work at all.
mysql> CREATE TABLE `new_products_ndb` (
-> `id` int(10) unsigned NOT NULL,
-> `extension_id` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `name` varchar(255) COLLATE latin1_general_ci NOT NULL,
-> `min_months` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `max_months` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `default_months` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `min_years` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `max_years` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `default_years` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `desctype` int(10) unsigned NOT NULL,
-> `type` enum(‘new’,'renewal’,'refund’) COLLATE latin1_general_ci DEFAULT NULL,
-> `renewal_service` varchar(25) COLLATE latin1_general_ci DEFAULT NULL,
-> `product_group_id` int(10) unsigned DEFAULT NULL,
-> `navision_code` varchar(32) COLLATE latin1_general_ci DEFAULT ”,
-> `pretty_name` varchar(255) COLLATE latin1_general_ci DEFAULT ”,
-> PRIMARY KEY (`id`,`extension_id`),
-> KEY `name` (`name`),
-> KEY `type` (`type`),
-> KEY `desctype` (`desctype`)
-> ) ENGINE=ndb DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
-> ;
Query OK, 0 rows affected (0.41 sec)
mysql> CREATE TABLE `customer_referral_commission_ndb` (
-> `product_id` int(10) unsigned NOT NULL,
-> `extension_id` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `customer_commission` decimal(9,2) unsigned DEFAULT ’0.00′,
-> `date_entered` datetime DEFAULT NULL,
-> `last_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-> PRIMARY KEY (`product_id`,`extension_id`),
-> KEY `product_id` (`product_id`),
-> KEY `extension_id` (`extension_id`),
-> CONSTRAINT `customer_referral_commission_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `new_products_ndb` (`id`)
-> ) ENGINE=ndb DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> CREATE TABLE `customer_referral_commission_ndb` (
-> `product_id` int(10) unsigned NOT NULL,
-> `extension_id` int(10) unsigned NOT NULL DEFAULT ’0′,
-> `customer_commission` decimal(9,2) unsigned DEFAULT ’0.00′,
-> `date_entered` datetime DEFAULT NULL,
-> `last_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-> PRIMARY KEY (`product_id`,`extension_id`),
-> KEY `product_id` (`product_id`),
-> KEY `extension_id` (`extension_id`),
-> CONSTRAINT `customer_referral_commission_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `new_products_ndb` (`id`) on delete cascade on update restrict
-> ) ENGINE=ndb DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
ERROR 1215 (HY000): Cannot add foreign key constraint
Also, another example where child table primary key is not composite but the master table has composite index, still doesn't work.
CREATE TABLE `webmail_quota` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`customer_id` int(10) unsigned NOT NULL,
`product_id` int(10) unsigned NOT NULL,
`mailbox_id` int(10) unsigned DEFAULT NULL,
`expires` date NOT NULL,
PRIMARY KEY (`id`),
KEY `customer_id` (`customer_id`),
KEY `product_id` (`product_id`),
KEY `mailbox_id` (`mailbox_id`),
CONSTRAINT `webmail_quota_ibfk_1` FOREIGN KEY(`customer_id`) REFERENCES `cust` (`uid`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=ndbcluster AUTO_INCREMENT=2932 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
mysql> alter table webmail_quota add CONSTRAINT `webmail_quota_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `new_products_ndb` (`id`) ON DELETE CASCADE ON UPDATE restrict;
ERROR 1215 (HY000): Cannot add foreign key constraint
This is very well supported in innodb. Can this be added in ndb as well.
How to repeat:
Just try the above on cluster and you can prove it yourself.
Suggested fix:
Please enable the same functionality as documentation says that this is not a limitation between innodb ad cluster.