MySQL queries working only on windows but no on linux - mysql

I have generated a script using Script-Migration and applied the script locally on the database and it worked on MySQL workbench on windows.
But once I tried to apply the same script on a cloned database on a Linux machine it keeps generating the error "Cannot add foreign key".
Edited
CREATE TABLE `NoticesOfSchoolReportForms` (
`Id` varchar(50) NOT NULL,
`EducationOfficeManagerName` varchar(50) NOT NULL,
`AssignmentNumber` int NOT NULL,
`Date` DateTime NOT NULL,
`ModifiedById` varchar(50) NULL,
`CreatedById` varchar(50) NOT NULL,
`ModificationDate` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`CreatedDate` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`Deleted` tinyint(1) NOT NULL DEFAULT FALSE,
`SupervisorId` varchar(50) NOT NULL,
`SchoolId` varchar(50) NOT NULL,
CONSTRAINT `PK_NoticesOfSchoolReportForms` PRIMARY KEY (`Id`),
CONSTRAINT `FK_NoticesOfSchoolReportForms_Schools_SchoolId` FOREIGN KEY (`SchoolId`) REFERENCES `Schools` (`Id`) ON DELETE CASCADE,
CONSTRAINT `FK_NoticesOfSchoolReportForms_SuperVisors_SupervisorId` FOREIGN KEY (`SupervisorId`) REFERENCES `SuperVisors` (`Id`) ON DELETE CASCADE
);

Related

mysql, results varying when I add a column to the select

Mysql version:
'innodb_version', '5.7.33'
'protocol_version', '10'
'slave_type_conversions', ''
'tls_version', 'TLSv1,TLSv1.1,TLSv1.2'
'version', '5.7.33-log'
'version_comment', 'Source distribution'
'version_compile_machine', 'x86_64'
'version_compile_os', 'Linux'
I have something very odd going on with a production mysql database that I can not suss out & I may struggle to explain. When I copy the database over to somewhere else everything is fine.
There are 1.5 million rows in method_profile & so far this is the only row I know to have this issue.
The problem is displayed in two ways. 1) why do I get back an invalid id of '-2147476736' & why do I get no rows back when I ad deleted_timestamp to the SQL.
This first select returns a row as I expect:
select mp.id, mp.screening_profile_id, mp.deleted, mp.deleted_timestamp
from method_profile mp
where mp.id = 481031;
So then using the screening_profile_id value I run the following SQL:
select mp.id, mp.screening_profile_id, mp.deleted from method_profile mp
where mp.screening_profile_id = 420062 ;
That id does not exist as shown by:
select mp.id from method_profile mp where mp.id = -2147476736;
If I change the second select to add another column 'mp.deleted_timestamp' I then get no row back at all:
select mp.id, mp.screening_profile_id, mp.deleted, mp.deleted_timestamp from method_profile mp
where mp.screening_profile_id = 420062 ;
The CREATE statement for the table is as follows:
CREATE TABLE `method_profile` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '5.1',
`primary_method_id` int(11) DEFAULT NULL,
`secondary_method_id` int(11) DEFAULT NULL,
`instrument_id` int(11) DEFAULT NULL,
`kit_code_id` int(11) DEFAULT NULL COMMENT '5.1 - allow NULL as option see http://youtrack/issue/PTPORTAL2-100',
`temperature_id` int(11) DEFAULT NULL,
`name` varchar(180) DEFAULT NULL,
`slope` decimal(10,3) DEFAULT NULL COMMENT '7.7',
`intercept` decimal(10,3) DEFAULT NULL COMMENT '7.7',
`deleted` bit(1) NOT NULL DEFAULT b'0',
`screening_profile_id` int(11) NOT NULL,
`round_id_valid_from` int(11) DEFAULT NULL,
`round_id_valid_to` int(11) DEFAULT NULL,
`measurement_unit_id` int(11) DEFAULT NULL,
`instrument_ref` varchar(20) DEFAULT NULL,
`generated_primary_method_id` int(11) GENERATED ALWAYS AS (ifnull(`primary_method_id`,0)) VIRTUAL NOT NULL,
`generated_secondary_method_id` int(11) GENERATED ALWAYS AS (ifnull(`secondary_method_id`,0)) VIRTUAL NOT NULL,
`generated_instrument_id` int(11) GENERATED ALWAYS AS (ifnull(`instrument_id`,0)) VIRTUAL NOT NULL,
`generated_instrument_ref` varchar(20) GENERATED ALWAYS AS (ifnull(`instrument_ref`,'')) VIRTUAL NOT NULL,
`generated_round_id_valid_from` int(11) GENERATED ALWAYS AS (ifnull(`round_id_valid_from`,0)) VIRTUAL NOT NULL,
`generated_round_id_valid_to` int(11) GENERATED ALWAYS AS (ifnull(`round_id_valid_to`,0)) VIRTUAL NOT NULL,
`generated_kit_code_id` int(11) GENERATED ALWAYS AS (ifnull(`kit_code_id`,0)) VIRTUAL NOT NULL,
`generated_temperature_id` int(11) GENERATED ALWAYS AS (ifnull(`temperature_id`,0)) VIRTUAL NOT NULL,
`generated_name` varchar(180) GENERATED ALWAYS AS (ifnull(`name`,'')) VIRTUAL NOT NULL,
`generated_slope` decimal(10,3) GENERATED ALWAYS AS (ifnull(`slope`,0.0)) VIRTUAL NOT NULL,
`generated_intercept` decimal(10,3) GENERATED ALWAYS AS (ifnull(`intercept`,0.0)) VIRTUAL NOT NULL,
`generated_measurement_unit_id` int(11) GENERATED ALWAYS AS (ifnull(`measurement_unit_id`,0)) VIRTUAL NOT NULL,
`inserted_at` datetime DEFAULT CURRENT_TIMESTAMP,
`last_updated` datetime DEFAULT CURRENT_TIMESTAMP,
`deleted_timestamp` timestamp NULL DEFAULT NULL,
`generated_deleted_timestamp` timestamp GENERATED ALWAYS AS (ifnull(`deleted_timestamp`,'0000-00-00 00:00:00')) VIRTUAL NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `method_profile_instrument_ref_uniq_idx` (`screening_profile_id`,`generated_primary_method_id`,`generated_secondary_method_id`,`generated_instrument_id`,`generated_instrument_ref`,`generated_round_id_valid_from`,`generated_round_id_valid_to`,`generated_kit_code_id`,`generated_temperature_id`,`generated_name`,`generated_slope`,`generated_intercept`,`generated_measurement_unit_id`,`deleted`,`generated_deleted_timestamp`),
KEY `fk_screening_profile_Kit_code1_idx` (`kit_code_id`),
KEY `fk_screening_profile_temperature1_idx` (`temperature_id`),
KEY `fk_screening_profile_method1_idx` (`primary_method_id`),
KEY `fk_screening_profile_method2_idx` (`secondary_method_id`),
KEY `fk_screening_profile_instrument1_idx` (`instrument_id`),
KEY `fk_method_profile_screening_profile` (`screening_profile_id`),
KEY `fk_method_profile_round_id_valid_from` (`round_id_valid_from`),
KEY `fk_method_profile_round_id_valid_to` (`round_id_valid_to`),
KEY `fk_method_profile_measurement_unit_id` (`measurement_unit_id`),
CONSTRAINT `fk_method_profile_instrument` FOREIGN KEY (`instrument_id`) REFERENCES `instrument` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_kit_code` FOREIGN KEY (`kit_code_id`) REFERENCES `kit_code` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_measurement_unit_id` FOREIGN KEY (`measurement_unit_id`) REFERENCES `measurement_units` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_primary_method` FOREIGN KEY (`primary_method_id`) REFERENCES `method` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_round_id_valid_from` FOREIGN KEY (`round_id_valid_from`) REFERENCES `round` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_round_id_valid_to` FOREIGN KEY (`round_id_valid_to`) REFERENCES `round` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_screening_profile` FOREIGN KEY (`screening_profile_id`) REFERENCES `screening_profile` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_secondary_method` FOREIGN KEY (`secondary_method_id`) REFERENCES `method` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_method_profile_temperature` FOREIGN KEY (`temperature_id`) REFERENCES `temperature` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1614629 DEFAULT CHARSET=utf8mb4;

shopware onetonone relationship error :General error: 1215 Cannot add foreign key constraint

I am trying to add 2 Tables to my database using these 2 queries, but I get this error on adding OnetoOne relationship, I am using shopware as framework to execute this queries.
The error says :
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key
constraint"
CREATE TABLE `texdata_productFields` (
`id` BINARY(16) NOT NULL,
`product_id` BINARY(16) NOT NULL,
`product_version_id` BINARY(16) NOT NULL,
`form_number` INT(11) NOT NULL,
`quality_number` INT(11) NOT NULL,
`color_number` INT(11) NOT NULL,
`color_group` INT(11) NULL,
`variant_number` INT(11) NOT NULL,
`size_value` VARCHAR(255) NOT NULL,
`size_format` VARCHAR(255) NULL,
`color_rgb_code` VARCHAR(255) NULL,
`color_group_rgb_code` VARCHAR(255) NULL,
`main_article_number` VARCHAR(255) NULL,
`care_symbols` VARCHAR(255) NULL,
`created_at` DATETIME(3) NOT NULL,
`updated_at` DATETIME(3) NULL,
PRIMARY KEY (`id`),
CONSTRAINT `json.texdata_productFields.translated` CHECK (JSON_VALID(`translated`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `texdata_productFields_translation` (
`materials` VARCHAR(255) NULL,
`colorGroupName` VARCHAR(255) NULL,
`variantDescription` VARCHAR(255) NULL,
`freiText1` VARCHAR(255) NULL,
`freiText2` VARCHAR(255) NULL,
`freiText3` VARCHAR(255) NULL,
`created_at` DATETIME(3) NOT NULL,
`updated_at` DATETIME(3) NULL,
`texdata_productFields_id` BINARY(16) NOT NULL,
`language_id` BINARY(16) NOT NULL,
PRIMARY KEY (`texdata_productFields_id`,`language_id`),
KEY `fk.texdata_productFields_translation.texdata_productFields_id` (`texdata_productFields_id`),
KEY `fk.texdata_productFields_translation.language_id` (`language_id`),
CONSTRAINT `fk.texdata_productFields_translation.texdata_productFields_id` FOREIGN KEY (`texdata_productFields_id`) REFERENCES `texdata_productFields` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `fk.texdata_productFields_translation.language_id` FOREIGN KEY (`language_id`) REFERENCES `language` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Language Table is a default table, this is the query that builds it:
CREATE TABLE `language` (
`id` BINARY(16) NOT NULL,
`parent_id` BINARY(16) NULL,
`locale_id` BINARY(16) NOT NULL,
`translation_code_id` BINARY(16) NULL,
`name` VARCHAR(255) NOT NULL,
`custom_fields` JSON NULL,
`created_at` DATETIME(3) NOT NULL,
`updated_at` DATETIME(3) NULL,
PRIMARY KEY (`id`),
CONSTRAINT `json.language.custom_fields` CHECK (JSON_VALID(`custom_fields`)),
KEY `fk.language.parent_id` (`parent_id`),
KEY `fk.language.locale_id` (`locale_id`),
KEY `fk.language.translation_code_id` (`translation_code_id`),
CONSTRAINT `fk.language.parent_id` FOREIGN KEY (`parent_id`) REFERENCES `language` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `fk.language.locale_id` FOREIGN KEY (`locale_id`) REFERENCES `locale` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `fk.language.translation_code_id` FOREIGN KEY (`translation_code_id`) REFERENCES `locale` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
You probably need to make sure that you have set up the texdata_productFields_id and language_idcolumn in the child table to a nullable column.
If you set
ON DELETE SET NULL
To your foreign key then it won't allow you to set the field as
NOT NULL

MySQL Foreign key error 1215, even though both colums are of the same type and are NOT NULL

Parent table team_entrant:
CREATE TABLE IF NOT EXISTS `team_entrant` (
`entrant_number` tinyint(4) NOT NULL,
`qualifying_position` tinyint(4) NOT NULL,
`qualifying_time` time(3) NOT NULL,
`grid_position` tinyint(4) DEFAULT NULL,
`best_race_time` datetime DEFAULT NULL,
`final_position` tinyint(4) DEFAULT NULL,
`dnf_reason` varchar(45) DEFAULT NULL,
`team_id` int(11) NOT NULL,
`competition_year` date NOT NULL,
PRIMARY KEY (`entrant_number`),
KEY `fk_team_entrant_team1_idx` (`team_id`),
CONSTRAINT `fk_team_entrant_team1` FOREIGN KEY (`team_id`) REFERENCES `team` (`team_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Child table/Associative table entrant_drivers:
CREATE TABLE IF NOT EXISTS `entrant_drivers` (
`entrant_number` tinyint(4) NOT NULL,
`competition_year` date NOT NULL,
`driver_id` int(11) NOT NULL,
PRIMARY KEY (`entrant_number`,`competition_year`,`driver_id`),
CONSTRAINT `ed_entrant_fk` FOREIGN KEY (`entrant_number`) REFERENCES `team_entrant` (`entrant_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
At the time the team_entrant column competition_year did not exist.
HeidiSQL refuses to execute the following code:
ALTER table entrant_drivers ADD CONSTRAINT ed_comp_year_fk FOREIGN KEY (competition_year) REFERENCES team_entrant(competition_year);
SQL Error (1215): Cannot add foreign key constraint
Extraneous table, driver involved with the associative table:
-- Dumping structure for table 99_lemans_db1.driver
CREATE TABLE IF NOT EXISTS `driver` (
`driver_id` int(11) NOT NULL,
`driver_name` varchar(64) NOT NULL,
`driver_nationality` varchar(32) NOT NULL,
`driver_birth_day` date NOT NULL,
`driver_best_previous_finish_class` varchar(8) DEFAULT NULL,
`driver_best_previous_finish_position` tinyint(4) DEFAULT NULL,
`team_entrant_id` int(11) NOT NULL,
PRIMARY KEY (`driver_id`,`team_entrant_id`),
KEY `fk_driver_team_entrant1_idx` (`team_entrant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Any assistance would be appreciated.
The parent column must be designated as an index/primary key.
team_entrant is supposed to be made up of the composite primary keys (entrant number, competition year).

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.

ERROR 1215: Cannot add foreign key constraint (multiple column FK)

I'm going crazy trying to figure out the reason of the well-known ERROR 1215 (MySQL 6.3.5).
I have to declare a table with a four-column PK, in which 3 of the columns are FK referencing other table.
CREATE TABLE `zen`.`pago_abono` (
`fecha_pago` DATETIME NOT NULL,
`cod_abono` VARCHAR(4) NOT NULL,
`mes` INT(2) NOT NULL,
`anio` YEAR NOT NULL,
`monto_pago` DECIMAL(10,2) NULL,
PRIMARY KEY (`fecha_pago`, `cod_abono`, `mes`, `anio`),
INDEX `cuota_fk_idx` (`mes` ASC, `anio` ASC, `cod_abono` ASC),
CONSTRAINT `cuota_fk`
FOREIGN KEY (`mes` , `anio` , `cod_abono`)
REFERENCES `zen`.`cuota_abono` (`mes` , `anio` , `cod_abono`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
"cuota_fk" references the table "cuota_abono"
CREATE TABLE `cuota_abono` (
`cod_abono` varchar(4) NOT NULL,
`mes` int(2) NOT NULL,
`anio` year(4) NOT NULL,
`valor` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`cod_abono`,`anio`,`mes`),
CONSTRAINT `cod_abono_fk` FOREIGN KEY (`cod_abono`) REFERENCES `abonos`(`cod_abono`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Finally, "cuota_abono" has a FK, "cod_abono_fk", referencing "abonos"
CREATE TABLE `abonos` (
`cod_abono` varchar(4) NOT NULL,
`direccion` varchar(45) NOT NULL,
`tel_verificacion` varchar(45) NOT NULL,
`fecha_alta` date DEFAULT NULL,
`descripcion` varchar(45) DEFAULT NULL,
`cuenta_cte` decimal(10,2) DEFAULT '0.00',
`cod_localidad` varchar(3) DEFAULT NULL,
`cod_cobrador` varchar(3) DEFAULT NULL,
`nombre_servicio` varchar(45) DEFAULT NULL,
PRIMARY KEY (`cod_abono`),
KEY `zona_cobranza_fk_idx` (`cod_localidad`,`cod_cobrador`),
CONSTRAINT `zona_cobranza_fk` FOREIGN KEY (`cod_localidad`, `cod_cobrador`) REFERENCES `zonas_cobranza` (`cod_localidad`, `cod_cobrador`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Can you find the mistake?
Thanks!
P.S.: All tables have already been created, except from "pago_abono". I'm showing the CREATE statement of tables "abonos" and "cuota_abono" just to let you know their structure.
I even tried creating "pago_abono" just with the PK and then used ALTER to add the FKs, but it still shows the error.