MySQL error 1022 when inserting. How to fix? - mysql

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 ...

Related

MySQL triger to set foreign key to null

I have table named Webpage_Categories (MySQL)
Webpage_Categories
-----------------
webpage_catgegory_id | parent_id | name
where parent_id field is foreign key, which references the webpage_category_id field.
Here is the create table statement:
CREATE TABLE `webpage_category` (
`webpage_category_id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) DEFAULT NULL,
`name` varchar(255) NOT NULL,
`name_en` varchar(255) DEFAULT NULL,
`thumbnail_image` varchar(255) DEFAULT NULL,
`thumbnail_image_en` varchar(255) DEFAULT NULL,
`display_date` tinyint(4) NOT NULL,
`display_thumbnail_image` tinyint(4) NOT NULL,
`display_short_text` tinyint(4) NOT NULL,
`display_more_button` tinyint(4) NOT NULL,
`imported_from_old_site` tinyint(4) NOT NULL,
`slug` varchar(255) NOT NULL,
`slug_en` varchar(255) DEFAULT NULL,
`sort_order` int(11) NOT NULL,
`visible` tinyint(4) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`deleted_by` int(11) DEFAULT NULL,
`deleted_from_ip` varchar(255) DEFAULT NULL,
`deleted_with_browser` varchar(255) DEFAULT NULL,
PRIMARY KEY (`webpage_category_id`),
KEY `Fk_User_WebpageCategory_idx` (`deleted_by`),
KEY `Fk_Parent_WebpageCategory_idx` (`parent_id`),
CONSTRAINT `Fk_Parent_WebpageCategory` FOREIGN KEY (`parent_id`) REFERENCES `webpage_category` (`webpage_category_id`) ON DELETE SET NULL ON UPDATE NO ACTION,
CONSTRAINT `Fk_User_WebpageCategory` FOREIGN KEY (`deleted_by`) REFERENCES `user` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb3;
Since I use soft delete in PHP, I want when a specific webpage category is deleted all rows that have set that webpage category as parent_id,those parent_id fields should be set to null.
I have the following code:
DELIMITER //
CREATE TRIGGER _set_webpage_category_foreign_key_to_null
AFTER UPDATE ON webpage_category
FOR EACH ROW
BEGIN
IF !(NEW.deleted_at <=> OLD.deleted_at) THEN
update webpage_category
set parent_id = NULL
WHERE parent_id = NEW.webpage_category_id;
END IF;
END; //
The error that I get in log file is: Can't update table 'webpage_category' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
Any help will be deeply appreciated.

Column of BIT datatype is not inserting/updating default values in MySql database using Spring Boot

So I have a table which is configured something like this
CREATE TABLE `jobDetails` (
`id` varchar(255) NOT NULL,
`job_id` varchar(255) NOT NULL,
`editor_id` varchar(255) DEFAULT NULL,
`editor_dueDate` datetime DEFAULT NULL,
`ops_tags` int NOT NULL DEFAULT '2',
`notes_from_ops` text CHARACTER SET utf8 COLLATE utf8_general_ci,
`reviewer_id` varchar(255) DEFAULT NULL,
`reviewer_dueDate` datetime DEFAULT NULL,
`sales_tags` varchar(255) DEFAULT '13',
`notes_from_sales` text CHARACTER SET utf8 COLLATE utf8_general_ci,
`shift` int DEFAULT NULL,
`additionalNotes` text CHARACTER SET utf8 COLLATE utf8_general_ci,
`editorStatus` int DEFAULT NULL,
`tracked_file_id` varchar(255) DEFAULT NULL,
`clean_file_id` varchar(255) DEFAULT NULL,
`additional_file_id` varchar(255) DEFAULT NULL,
`editor_wordCount` int DEFAULT NULL,
`job_details_type` int DEFAULT '0',
`editing_speed` int DEFAULT '0',
`reviewing_priority` int DEFAULT NULL,
`is_enabled` bit(1) NOT NULL DEFAULT b'1',
PRIMARY KEY (`id`),
KEY `FKhb282r593ce4xojoo4hfd3fwm` (`additional_file_id`),
KEY `FKnb8b8nhc9yb78e2p5ipadfc8u` (`clean_file_id`),
KEY `FK82594hccbyjik7bc0ev4f74ks` (`tracked_file_id`),
CONSTRAINT `FK82594hccbyjik7bc0ev4f74ks` FOREIGN KEY (`tracked_file_id`) REFERENCES `files` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `FKhb282r593ce4xojoo4hfd3fwm` FOREIGN KEY (`additional_file_id`) REFERENCES `files` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `FKnb8b8nhc9yb78e2p5ipadfc8u` FOREIGN KEY (`clean_file_id`) REFERENCES `files` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1
So when I insert/update into this table through my code without setting its value then it should consider a default value to "true" or "1", but currently this is not the case it sets the value to "0" or "false".
My backend is using Spring Boot and Spring Data JPA
Without seeing your code might be difficult to answer, but here's a theory: you are (auto)initializing primitives (int or boolean) with their default value (0 or false), which is not null, so the DEFAULT keyword is ignored. You should also define default column values in Java, as shown here.

MySQL Workbench error 1452

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.

Regex to get names of all SQL tables

I have a CREATE TABLE document and I need to get a listing of all the table names. This is what I have:
CREATE TABLE `mturk_reviewqueue` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`catalog_id` int(11) DEFAULT NULL,
`tv_series_id` int(11) DEFAULT NULL,
`added_on` datetime NOT NULL,
`correct_url` varchar(100) DEFAULT NULL,
`notes` varchar(400) DEFAULT NULL,
`completed_on` datetime DEFAULT NULL,
`completed_by_id` int(11) DEFAULT NULL,
`top_url` varchar(100) DEFAULT NULL,
`diff_score` decimal(5,2) DEFAULT NULL,
`ip_checkout` varchar(24) DEFAULT NULL,
`incorrect_original_url` varchar(100) DEFAULT NULL,
`fix_notes` varchar(1000) DEFAULT NULL,
`is_promotional_content` tinyint(1) DEFAULT NULL,
`contains_multiple_titles` tinyint(1) DEFAULT NULL,
`is_garbage_series` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `completed_by` (`completed_by_id`),
KEY `catalog_id` (`catalog_id`),
KEY `tv_series_id` (`tv_series_id`),
CONSTRAINT `mturk_reviewqueue_ibfk_1` FOREIGN KEY (`completed_by_id`) REFERENCES `auth_user` (`id`),
CONSTRAINT `mturk_reviewqueue_ibfk_2` FOREIGN KEY (`catalog_id`) REFERENCES `main_catalog` (`id`),
CONSTRAINT `mturk_reviewqueue_ibfk_3` FOREIGN KEY (`tv_series_id`) REFERENCES `main_tvseriesinstance` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=196089 DEFAULT CHARSET=utf8;
-- Create syntax for TABLE 'mturk_worker'
CREATE TABLE `mturk_worker` (
`worker_id` varchar(22) NOT NULL DEFAULT '',
`notes` varchar(100) NOT NULL,
...etc...
And this is what I need:
mturk_reviewqueue
mturk_worker
etc...
So far I have:
r'CREATE\sTABLE\s`(.+)`\s\('
This is getting me the table name, but I can't seem to get rid of all the junk after the table name but before the next one. What would be the best regex to do this?
r'CREATE\sTABLE\s`(.+?)`'
This should do it for you.
If you'd like a command line option. Say your sql file is named "foo":
cat foo | grep 'CREATE TABLE' | cut -d\` -f2
This should work on MacOS.
This should also work (even when there should be more than one blank between words) ...
/CREATE\s+TABLE\s+`([^`]+)`/g
If you're using ack you can do this:
ack 'CREATE TABLE `(.+)`' --output='$1'

PhpMyAdmin export : reference without uppercase

First, I'm sorry for my poor English, I'm french.
I use PhpMyAdmin with Xampp in localhost and an other PhpMyAdmin on server
My tables are like Personne, Adresse, Titre.
When I use the export option with server my sql is perfect :
CREATE TABLE IF NOT EXISTS `Personne` (
`idPersonne` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`gsm` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`nom` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`prenom` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`telephone` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`idAdresse` int(11) DEFAULT NULL,
`idTitre` int(11) DEFAULT NULL,
PRIMARY KEY (`idPersonne`),
UNIQUE KEY `uniquePersonne` (`nom`,`prenom`),
KEY `IDX_F6B8ABB9D3E663E4` (`idAdresse`),
KEY `IDX_F6B8ABB9E8B304A9` (`idTitre`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=685 ;
ALTER TABLE `Personne`
ADD CONSTRAINT `FK_F6B8ABB9D3E663E4` FOREIGN KEY (`idAdresse`) REFERENCES `Adresse` (`idAdresse`) ON DELETE SET NULL,
ADD CONSTRAINT `FK_F6B8ABB9E8B304A9` FOREIGN KEY (`idTitre`) REFERENCES `Titre` (`idTitre`) ON DELETE SET NULL;
By when I use the local export
CREATE TABLE IF NOT EXISTS `Personne` (
`idPersonne` int(11) NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`gsm` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`nom` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`prenom` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`telephone` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`idAdresse` int(11) DEFAULT NULL,
`idTitre` int(11) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=685 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
it's makes lines for primary, unique, normal key and auto_increment :
ALTER TABLE `Personne`
ADD PRIMARY KEY (`idPersonne`), ADD UNIQUE KEY `uniquePersonne` (`nom`,`prenom`), ADD KEY `IDX_F6B8ABB9D3E663E4` (`idAdresse`), ADD KEY `IDX_F6B8ABB9E8B304A9` (`idTitre`);
ALTER TABLE `Personne`
MODIFY `idPersonne` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=685;
And reference don't contain uppercase
ALTER TABLE `Personne`
ADD CONSTRAINT `FK_F6B8ABB9D3E663E4` FOREIGN KEY (`idAdresse`) REFERENCES `adresse` (`idAdresse`) ON DELETE SET NULL,
ADD CONSTRAINT `FK_F6B8ABB9E8B304A9` FOREIGN KEY (`idTitre`) REFERENCES `titre` (`idTitre`) ON DELETE SET NULL;
How have the same result of server?
Thank you for your help
Adjust your lower_case_table_names setting for MySQL to achieve your goal. See How to force case sensitive table names?