Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selected columns dont have compatiable type, even it has same type

I am creating FOREIGN KEY on Table1(propType) from Table2 with groupName. But i am facing below error. selected columns dont have compatiable type , even it has same type in mysql

Table 1 :

    CREATE TABLE `configuration_master` (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `insertTimestamp` datetime DEFAULT NULL,
      `propName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
      `propValue` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `propType` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
      `IsCloudSupport` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
      `DisplayName` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
      `updateTimestamp` datetime DEFAULT NULL,
      `userId` bigint(20) DEFAULT NULL,
      `SYNCCOL1` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL3` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL4` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL6` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL7` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL8` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL9` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `SYNCCOL10` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
      `prptyp` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `propName` (`propName`),
      KEY `FKB54491EB8E326E43` (`userId`),
      KEY `proptype_frn_idx` (`propType`),
      KEY `ASD_idx` (`prptyp`),
      CONSTRAINT `FKB54491EB8E326E43` FOREIGN KEY (`userId`) REFERENCES `user_master` (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=86 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Table2:

    CREATE TABLE `config_group` (
      `groupName` varchar(45) CHARACTER SET utf8 NOT NULL,
      `prop1` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
      `prop2` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
      `prop3` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
      `prop4` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
      `ADS` int(11) DEFAULT NULL,
      `config_groupcol` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
      PRIMARY KEY (`groupName`),
      UNIQUE KEY `config_groupcol_UNIQUE` (`config_groupcol`),
      UNIQUE KEY `ADS_UNIQUE` (`ADS`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

I am creating FOREIGN KEY as below

    ALTER TABLE `konysyncconsoledb7214dev`.`configuration_master` 
    ADD CONSTRAINT `FK_configGrop`
      FOREIGN KEY (`propType`)
      REFERENCES `konysyncconsoledb7214dev`.`config_group` (`groupName`)

Below image is error msg. if any faced this issue plz let me know what i am missing. error msg

like image 395
Yugandhar Avatar asked Jun 02 '16 15:06

Yugandhar


2 Answers

From the mysql manual page found here

Corresponding columns in the foreign key and the referenced key must have similar data types. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.

Your issue was the collation. I had no problem creating the following whatsoever. Note that I had to ditch your db name from part of it.

Oh, and since you did not provide the third table, I had to remove that FK constraint. But that is not the issue.

Note, nullability was not the issue.

What you decide to do about your collation choices is up to you. But that was it.

CREATE TABLE `configuration_master` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `insertTimestamp` datetime DEFAULT NULL,
  `propName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `propValue` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `propType` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `IsCloudSupport` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `DisplayName` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `updateTimestamp` datetime DEFAULT NULL,
  `userId` bigint(20) DEFAULT NULL,
  `SYNCCOL1` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL3` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL4` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL6` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL7` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL8` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL9` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `SYNCCOL10` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `prptyp` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `propName` (`propName`),
  KEY `FKB54491EB8E326E43` (`userId`),
  KEY `proptype_frn_idx` (`propType`),
  KEY `ASD_idx` (`prptyp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



 CREATE TABLE `config_group` (
  `groupName` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
  `prop1` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  `prop2` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  `prop3` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  `prop4` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  `ADS` int(11) DEFAULT NULL,
  `config_groupcol` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`groupName`),
  UNIQUE KEY `config_groupcol_UNIQUE` (`config_groupcol`),
  UNIQUE KEY `ADS_UNIQUE` (`ADS`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;   

ALTER TABLE  `configuration_master` 
ADD CONSTRAINT `FK_configGrop`
  FOREIGN KEY (`propType`)
  REFERENCES `config_group` (`groupName`);

More detail of your Original

This addresses the comment from Barmar. Your create table for config_group caused a discrepancy for matching collations for the FK to succeed with the ALTER TABLE . Manual page for SHOW FULL COLUMNS

From the Manual Page here:

If CHARACTER SET X is specified without COLLATE, character set X and its default collation are used.

CREATE TABLE t1
(
    col1 CHAR(10) CHARACTER SET utf8
) CHARACTER SET latin1 COLLATE latin1_bin;

The character set is specified for the column, but the collation is not. The column has character set utf8 and the default collation for utf8, which is utf8_general_ci. To see the default collation for each character set, use the SHOW COLLATION statement.

enter image description here

like image 68
Drew Avatar answered Sep 21 '22 00:09

Drew


propType is by default NULL, but groupName CANNOT be NULL. This could be your issue.

`propType` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL

`groupName` varchar(45) CHARACTER SET utf8 NOT NULL

But I may be wrong. Try looking over these guidelines and ensuring your FK constraint follows all of them:

  1. Both tables must be InnoDB type.
  2. In the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.
  3. Index prefixes on foreign key columns are not supported.
  4. InnoDB needs indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan.
  5. The two key fields must have the compatible field type.
  6. The size and the sign of integer types has to be the same.
  7. The length of string types need not be the same.
  8. The foreign key name must be unique within the database
  9. If you specify a SET NULL action, make sure that you have not declared the columns in the child table as NOT NULL.

(From here: http://wiki.navicat.com/wiki/index.php/Why_I_cannot_successfully_create_the_foreign_keys%3F)

like image 24
kayla210 Avatar answered Sep 19 '22 00:09

kayla210