Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint. Note that the internal storage type of ENUM and SET changed in tables created with >= InnoDB-4.1.12, and such columns in old tables cannot be referenced by such columns in new tables.
Above is the full description of error im getting. i checked the indexes of both table and should be fine but it still gives me this error.
Below is the tables structure.
CREATE TABLE `contact_address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`address_type` int(5) DEFAULT NULL,
`address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`postcode` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`area` int(5) DEFAULT NULL,
`state` int(5) DEFAULT NULL,
`country` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`phone` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`fax` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`email` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`contact_branch_id` int(11) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` int(4) DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`updated_by` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_c_a_area` (`area`),
KEY `fk_c_a_state` (`state`),
KEY `fk_c_a_country` (`country`),
KEY `fk_c_a_contact_branch_id` (`contact_branch_id`),
KEY `fk_c_a_created_by` (`created_by`),
CONSTRAINT `fk_c_a_area` FOREIGN KEY (`area`) REFERENCES `ref_area` (`area_id`),
CONSTRAINT `fk_c_a_contact_branch_id` FOREIGN KEY (`contact_branch_id`) REFERENCES `contact_branch` (`id`),
CONSTRAINT `fk_c_a_created_by` FOREIGN KEY (`created_by`) REFERENCES `user` (`id`),
CONSTRAINT `fk_c_a_state` FOREIGN KEY (`state`) REFERENCES `ref_state` (`state_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `ref_countries` (
`country_code` char(2) NOT NULL,
`country_id` int(11) NOT NULL AUTO_INCREMENT,
`country_name` varchar(80) NOT NULL,
`alpha_3` char(3) DEFAULT NULL,
`calling_code` int(5) NOT NULL,
`continent_name` varchar(30) DEFAULT NULL,
PRIMARY KEY (`country_code`),
KEY `country_id` (`country_id`)
) ENGINE=InnoDB AUTO_INCREMENT=253 DEFAULT CHARSET=utf8mb4;
Im trying to run this and giving me the error.
ALTER TABLE `contact_address`
ADD CONSTRAINT `fk_c_a_countries` FOREIGN KEY (`country`)
REFERENCES `ref_countries` (`country_code`);
The error message you're getting suggests that there's a problem with the foreign key constraint that you're trying to add to the contact_address table. Based on the table definitions you've provided, there are a couple of things that could be causing the issue:
The data type of the country column in the contact_address table is char(2), while the data type of the country_code column in the ref_countries table is char(2) NOT NULL. The NOT NULL constraint on the country_code column may be causing the problem.
Another possibility is that the ref_countries table have not have AUTO_INCREMENT on country_code column, but the contact_address table's country column is referencing that.
You can try the following:
Remove the NOT NULL constraint from the country_code column in the ref_countries table.
Add AUTO_INCREMENT to the country_code column in the ref_countries table.
Also, make sure that there is a matching value in the ref_countries table for each value of the country column in the contact_address table.
For a back-end web project, we use PHP 7.4 and MySQL 8.X, here is the structure of a part of the DB
CREATE TABLE `availability ` (
`id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
`student_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`start` date DEFAULT NULL,
`end` date DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_2CBACE2FDDEAB1A3` (`student_id`),
CONSTRAINT `FK_2CBACE2FDDEAB1A3` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
CREATE TABLE `student` (
`id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`sex_id` int DEFAULT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`first_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`phone` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`picture` longtext COLLATE utf8mb4_unicode_ci,
`cv` longtext COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`id`),
KEY `IDX_717E22E3448F3B3C` (`sex_id`),
CONSTRAINT `FK_717E22E3448F3B3C` FOREIGN KEY (`sex_id`) REFERENCES `sex` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
CREATE TABLE `request` (
`id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`student_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`date` datetime NOT NULL,
`title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`content` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_2694D7A5DDEAB1A3` (`student_id`),
CONSTRAINT `FK_2694D7A5DDEAB1A3` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Here are the cardinalities:
a STUDENT can have several availabilities (no matter what the
category of the availability is) an AVAILABILITY can belong to one or
more STUDENTS
a student can have one or more APPLICATIONS
a REQUEST belongs to only one STUDENT
but when I try to retrieve all the REQUESTS according to a period, it retrieves the data twice:
SELECT DE.*, ET.picture AS picture_editor, CONCAT(ET.first_name, ' ', ET.name) AS name_editeur, DI.start, DI.end
FROM request DE
INNER JOIN student ET ON ET.id = DE.student_id
INNER JOIN availability DI ON DI.student_id = DE.student_id
WHERE DI.start< CAST('2022-12-31' AS DATETIME) AND DI.end> CAST('2022-09-11' AS DATETIME)
Can you help me as I want to retrieve the data according to the student's availability
I have very strange problem with my MariaDB Database.
I am using it in pair with gorm
When I try to make an insert request to one of the tables I receive error
Error 1022: Can't write; duplicate key in table 'titles'
Ok. I try to use the same code directly from Navicat or Datagrip and MAGIC - everything is working and record inserting. I have checked foreign keys - all have unique names, but I still don't know how even model this.
Definition of table
/*
Navicat MariaDB Data Transfer
Source Server : My Hetzner
Source Server Version : 100316
Source Host : 127.0.0.1:3306
Source Database : sovet_api
Target Server Type : MariaDB
Target Server Version : 100316
File Encoding : 65001
Date: 2020-05-09 18:46:52
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for titles
-- ----------------------------
DROP TABLE IF EXISTS `titles`;
CREATE TABLE `titles` (
`anime_id` int(11) NOT NULL,
`anime_name` varchar(255) DEFAULT NULL,
`anime_name_rus` varchar(255) DEFAULT NULL,
`ai_help` varchar(255) DEFAULT NULL,
`pidor_ban` int(11) DEFAULT 0,
`vk_new_template_sub` text CHARACTER SET utf8mb4 DEFAULT NULL,
`vk_new_template_dub` text CHARACTER SET utf8mb4 DEFAULT NULL,
`video_name_template_sub` varchar(255) DEFAULT '',
`video_name_template_dub` varchar(255) DEFAULT '',
`video_desc_template_sub` text DEFAULT NULL,
`video_desc_template_dub` text DEFAULT NULL,
`telegram_new_template_sub` text CHARACTER SET utf8mb4 DEFAULT NULL,
`telegram_new_template_dub` text CHARACTER SET utf8mb4 DEFAULT NULL,
`telegram_sub_group_id` bigint(20) DEFAULT -1001269855704,
`telegram_dub_group_id` bigint(20) DEFAULT -1001269855704,
`vk_banned` int(11) DEFAULT 0,
`command_sub_id` int(11) DEFAULT 1,
`command_dub_id` int(11) DEFAULT 1,
`default_preroll_id_sub` int(11) DEFAULT NULL,
`default_watermark_id_sub` int(11) DEFAULT NULL,
`default_preroll_id_dub` int(11) DEFAULT NULL,
`default_watermark_id_dub` int(11) DEFAULT NULL,
`vk_sub_group_id` bigint(20) DEFAULT 33905270,
`vk_dub_group_id` bigint(20) DEFAULT 33905270,
`vk_album_id_sub` int(11) DEFAULT NULL,
`vk_album_id_dub` int(11) DEFAULT NULL,
`repost_sub_vk_group_id` int(11) DEFAULT 0,
`repost_dub_vk_group_id` int(11) DEFAULT 0,
`default_sub_add_att` text DEFAULT NULL,
`default_dub_add_att` text DEFAULT NULL,
`gdrive_parent_id` varchar(255) DEFAULT NULL,
`gdrive_torrent_id` varchar(255) DEFAULT NULL,
`gdrive_sub_id` varchar(255) DEFAULT NULL,
`gdrive_font_id` varchar(255) DEFAULT NULL,
`ai_search_enabled` int(11) DEFAULT 1,
`nyaa_scan_enabled` int(11) DEFAULT 0,
`anime_shiki_id` int(11) DEFAULT NULL,
`anime_name_jpn` varchar(255) DEFAULT NULL,
`anime_rating` varchar(255) DEFAULT NULL,
`anime_episodes` int(255) DEFAULT 0,
`hashtags` text DEFAULT NULL,
`subscribe_sub_enabled` smallint(6) DEFAULT 0,
`subscribe_dub_enabled` smallint(6) DEFAULT 0,
`subscribe_priority` smallint(6) DEFAULT 0,
`sub_purse` bigint(255) DEFAULT NULL,
`dub_purse` bigint(255) DEFAULT NULL,
`sub_qiwi` varchar(255) DEFAULT NULL,
`dub_qiwi` varchar(255) DEFAULT NULL,
`sub_paypal` varchar(255) DEFAULT NULL,
`dub_paypal` varchar(255) DEFAULT NULL,
PRIMARY KEY (`anime_id`),
KEY `command_sub_id` (`command_sub_id`),
KEY `command_dub_id` (`command_dub_id`),
KEY `default_watermark_id_sub` (`default_watermark_id_sub`),
KEY `default_watermark_id_dub` (`default_watermark_id_dub`),
KEY `default_preroll_id_sub` (`default_preroll_id_sub`),
KEY `default_preroll_id_dub` (`default_preroll_id_dub`),
FULLTEXT KEY `IDX__titles__anime_name_search` (`anime_name`,`ai_help`),
FULLTEXT KEY `IDX__titles__anime_name_search_bot` (`anime_name`,`anime_name_rus`,`ai_help`),
CONSTRAINT `titles_ibfk_1` FOREIGN KEY (`command_sub_id`) REFERENCES `command` (`command_id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `titles_ibfk_2` FOREIGN KEY (`command_dub_id`) REFERENCES `command` (`command_id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `titles_ibfk_3` FOREIGN KEY (`default_watermark_id_sub`) REFERENCES `watermarks` (`watermark_id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `titles_ibfk_4` FOREIGN KEY (`default_watermark_id_dub`) REFERENCES `watermarks` (`watermark_id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `titles_ibfk_5` FOREIGN KEY (`default_preroll_id_sub`) REFERENCES `prerolls` (`preroll_id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `titles_ibfk_6` FOREIGN KEY (`default_preroll_id_dub`) REFERENCES `prerolls` (`preroll_id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and
DROP TRIGGER IF EXISTS `trigger_hash_gen`;
DELIMITER ;;
CREATE TRIGGER `trigger_hash_gen` AFTER INSERT ON `titles`
FOR EACH ROW
IF NOT EXISTS (SELECT * FROM title_hashtags
WHERE hashtag_value = TRIM(BOTH '_' FROM
REGEXP_REPLACE(NEW.anime_name, '[^a-zA-Z0-9]+','_')))
THEN
INSERT INTO title_hashtags(anime_id, hashtag_value)
VALUES (NEW.anime_id, TRIM(BOTH '_' FROM
REGEXP_REPLACE(NEW.anime_name, '[^a-zA-Z0-9]+','_')));
END IF
;;
DELIMITER ;
The SQL-code
INSERT INTO `titles` (`anime_id`,`anime_name`,`anime_name_rus`,`pidor_ban`,`vk_new_template_sub`,`vk_new_template_dub`,`video_name_template_sub`,`video_name_template_dub`,`video_desc_template_sub`,`video_desc_template_dub`,`telegram_new_template_sub`,`telegram_new_template_dub`,`telegram_sub_group_id`,`telegram_dub_group_id`,`vk_banned`,`vk_sub_group_id`,`vk_dub_group_id`,`vk_album_id_sub`,`vk_album_id_dub`,`default_sub_add_att`,`default_dub_add_att`,`gdrive_parent_id`,`gdrive_torrent_id`,`gdrive_sub_id`,`gdrive_font_id`,`anime_shiki_id`,`anime_name_jpn`,`anime_rating`,`anime_episodes`,`subscribe_sub_enabled`,`subscribe_dub_enabled`,`subscribe_priority`,`sub_purse`,`dub_purse`,`sub_qiwi`,`dub_qiwi`,`sub_paypal`,`dub_paypal`,`ai_search_enabled`,`nyaa_scan_enabled`)
VALUES ('1046','Urashimasakatasen no Nichijou Special','Деньки Урасимасакатасэн: Школьная пора — Эпизод 13','0','','','','','','','','','0','0','0','0','0','56053731','56053731','','','1-SHGDPX_ybF0Kqu0rtLKcnY_rmfsXa96','1WmRjv8fsZAAY2uBGoRx76Ar1B8rJGKcj','1TvlCnAQ6DbLR3WWiePDCJerYNEwX09oW','1nZRP4Yf92DyuyeOPtW44_vzZLENN6gs6','40921','','','0','0','0','0','0','0','','','','','0','0')
But when I try to use THE SAME SQL in datagrip, navicat or smth other - it works
Thank you!
How about not doing the IF...SELECT; simply do INSERT IGNORE ....
Or maybe INSERT ... ON DUPLICATE KEY UPDATE ...
I've trying to create a foreign key from a table to another, using the tools that MySQL Workbench provides, but all that I get is this error:
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (`mediacom`.`#sql-758_4`, CONSTRAINT `med_agente_ibfk_1` FOREIGN KEY (`id_agenzia`) REFERENCES `med_agenzia` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
ALTER TABLE `mediacom`.`med_agente`
ADD CONSTRAINT `med_agente_ibfk_1`
FOREIGN KEY (`id_agenzia`)
REFERENCES `mediacom`.`med_agenzia` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
The thing that confuses me the most is the line mediacom.#sql-758_4, since the real query that I use is :
ALTER TABLE `mediacom`.`med_agente`
ADD INDEX `med_agente_ibfk_1_idx` (`id_agenzia` ASC) COMMENT '';
ALTER TABLE `mediacom`.`med_agente`
ADD CONSTRAINT `med_agente_ibfk_1`
FOREIGN KEY (`id_agenzia`)
REFERENCES `mediacom`.`med_agenzia` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
The index is inserted fine but the rest is ignored, why?
This is the med_agente table
CREATE TABLE `med_agente` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(225) DEFAULT NULL,
`cognome` varchar(225) DEFAULT NULL,
`disabilitato` tinyint(1) DEFAULT NULL,
`mod_time` datetime DEFAULT NULL,
`mod_user` varchar(255) DEFAULT NULL,
`codmobile` decimal(13,0) DEFAULT NULL,
`codfisso` decimal(13,0) DEFAULT NULL,
`id_agenzia` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `med_agente_ibfk_1_idx` (`id_agenzia`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
And this is med_agenzia
CREATE TABLE `med_agenzia` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ragSociale` varchar(255) NOT NULL,
`descrizione` varchar(255) DEFAULT NULL,
`indirizzo` varchar(255) DEFAULT NULL,
`citta` varchar(255) DEFAULT NULL,
`CAP` int(11) DEFAULT NULL,
`provincia` varchar(255) DEFAULT NULL,
`tel` int(11) DEFAULT NULL,
`mail` varchar(255) DEFAULT NULL,
`codFiscale` varchar(255) DEFAULT NULL,
`pIVA` varchar(255) DEFAULT NULL,
`id_azsuper` int(11) DEFAULT NULL,
`disabilitato` tinyint(1) DEFAULT NULL,
`mod_time` datetime DEFAULT NULL,
`mod_user` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
where's the problem???
Before you run your query
Run this :
SET FOREIGN_KEY_CHECKS=0;
Then set it to 1
SET FOREIGN_KEY_CHECKS=1;
after you run your Alter query.
I'm trying to add a foreign key constraint on my schema devair from field user_id of table bluePrints to the PK id of table users but I get an error:
ERROR 1215: Cannot add foreign key constraint
Here are my table definitions:
users:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
`remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
bluePrints:
CREATE TABLE `bluePrints` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`bluePrintName` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`user_id` int(10) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `uid` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
And the offending alter table statement:
ALTER TABLE `devair`.`bluePrints`
ADD CONSTRAINT `bp_u`
FOREIGN KEY (user_id)
REFERENCES `devair`.`users` (id)
ON DELETE CASCADE
ON UPDATE CASCADE;
The simple answer would be that you need to do this:
ALTER TABLE table1 ADD CONSTRAINT fk_bp_id FOREIGN KEY (columnFromTable1) REFERENCES table2(columnReferencedFromTable2);
The problem is the 'unsigned' in the bluePrints table. Once it's unsigned, then it works.
Now i have to figure out how to get laravel to use unsigned.