I am trying to migrate a MariaDB database to MySQL. To do this I have created a mysqldump of the MariaDB database and am attempting to import into MySQL. (This is being done with the official docker container mysql:latest).
I am getting the following error when importing into MySQL:
ERROR 3822 (HY000) at line 172: Duplicate check constraint name 'CONSTRAINT_1'.
If I look at the mysqldump file I can see why this is happening. All boolean columns in my database have a constraints that looks something like:
CONSTRAINT `CONSTRAINT_1` CHECK (`bool_col_1` in (0,1))
CONSTRAINT `CONSTRAINT_2` CHECK (`bool_col_2` in (0,1))
CONSTRAINT `CONSTRAINT_3` CHECK (`bool_col_3` in (0,1))
These constraints were not explicitly created by me but implicitly by Flask-SQLAlchemy (I think).
Notice how the constraint names are incremented starting with CONSTRAINT_1. Well the problem is that each table starts incrementing its constraint names starting with CONSTRAINT_1. Thus the error I am seeing gets thrown when trying to create the second table.
I'd like to know how best to solve this while keeping the integrity of my database intact.
Related
I dropped an entire database and am now trying to create a new database of the same name (a modified version of the old one, using an install script in case you are wondering why I am doing this).
The database fails on creation with a foreign key constraint (which is strange as the line of the sql file is way before I add constraints - the last time in the sql file).
Even stranger if I look in "information_schema" in the referential_constraints table I see the constraints still there from the schema I had dropped recently.
I am running MySQL community servers 8.0.22 under macOS Catalina. I have never had this behaviour that a constraint is free floating without an active schema that later invokes itself if you create a schema with that same name (note if I change the database name to any other name the SQL script loads no problem and creates the entire db)
I am trying to make a relational database in MYSQL.
Currently, I am making the foreign keys and connecting them to the parent table.
The problem is when I try to do this(in MYSQL workbench) MYSQL adds this line of code:
ADD INDEX `FK_party_coalitionparty_idx` (`partyId` ASC) VISIBLE;
After some research, I found out that it does this because when I delete or update the parent table, it is really handy when the child tables also delete or update the connected values(or columns).
The problem is when I run the foreign key code without the add index line it runs without trouble, but with it added (and I think I understand why it is good to have it added) it errors and does not want to execute the code to update my database.
When I try to execute the code in a SQL file it gives me the following error with the word VISIBLE:
VISIBLE is not valid at this position.
When I only try to delete the visible word, it cannot add my constraint (I think because you cannot put 2 times add below each other). I will include some screenshots and the message log to make my problem more clear.
Message log:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
ALTER TABLE `testdatabase`.`coalitionparty`
ADD INDEX `FK_party_coalitionparty_idx` (`partyId` ASC) VISIBLE;
;
ALTER TABLE `testdatabase`.`coalitionparty`
ADD CONSTRAINT `FK_party_coalitionparty`
FOREIGN KEY (`partyId`)
REFERENCES `testdatabase`.`party` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2
SQL Statement:
ALTER TABLE `testdatabase`.`coalitionparty`
ADD INDEX `FK_party_coalitionparty_idx` (`partyId` ASC) VISIBLE
My SQL File (when I try to run the SQL code but not with the workbench menu:
SQL file with the error included at the bottom
The question:
How do I need to fix this problem, so that I am able to use cascade and I don't get the error?
Thanks in advance!
Mariadb has no VISIBLE
check the manual for more information
So you can only do
ALTER TABLE `coalitionparty`
ADD INDEX `FK_party_coalitionparty_idx` (`partyId` ASC) ;
or switch to MySsQL
updateSchema runs well with an empty database, but the second time I get the following MySQL error:
SQLSTATE[HY000]: General error: 1025 Error on rename of
'./mydatabase/#sql-7f5_2b' to
'./mydatabase/mytable' (errno: 150)
According to a quick search this error happens on foreign constraint violations. The right approach would be for doctrine to disable foreign key checks when ALTERing a table.
Is there something I can do about this (besides patching Dcotrine)?
Further more I'm specifying:
'engine' => 'myisam',
... in the connectionOptions, but it gets ignored.
Edit:
When I remove foreign keys from other tables containing reference to mytable the error won't happen (it will happen with the next table which is still referenced by FKs but not with mytable).
Unfortunately, Doctrine doesn't handle this situation completely right.
You should disable the constraints by yourself and let Doctrine to recreate them.
To disable constraints, connect to MySQL and type:
SHOW CREATE TABLE mytable;
It will give you the SQL needed to create the table, where you'll see the constraint creation directive. Suppose the constraint is called 'mytable_fk', then you need to issue the command:
ALTER TABLE mytable
DROP CONSTRAINT mytable_fk;
The next time you run updateSchema, Doctrine will detect the missing restriction and will recreate it if needed.
I followed the advice here, but I'm still running into the same issues after importing my database to a local XAMPP installation.
My sql dump is wrapped in these tags:
SET FOREIGN_KEY_CHECKS=0;
// FULL DB DUMP
SET FOREIGN_KEY_CHECKS=1;
Even still, I get this error message:
Error
SQL query:
ALTER TABLE `mage_catalog_eav_attribute` ADD CONSTRAINT `FK_CATALOG_EAV_ATTRIBUTE_ID` FOREIGN KEY ( `attribute_id` ) REFERENCES `mage_eav_attribute` ( `attribute_id` ) ON DELETE CASCADE ON UPDATE CASCADE ;
MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`myDB_NAME`.<result 2 when explaining filename '#sql-2e0_5a'>, CONSTRAINT `FK_CATALOG_EAV_ATTRIBUTE_ID` FOREIGN KEY (`attribute_id`) REFERENCES `mage_eav_attribute` (`attribute_id`) ON DE)
Can anyone help me understand what else I would need to disable these checks, or prevent this error?
I am running Magento 1.4.2, and importing with phpmyadmin via xampp.
When I took a phpMyAdmin MySQL export and put it into another phpMyAdmin interface, I found that pre-existing tables caused the constraint problem. Deleting the tables was also problematic because of the key constraints.
When importing, make sure the present database tables do not have key constraints. I did this by dropping the tables.
To drop the tables, I turned off the constraint check by executing SQL code like this in the SQL screen of phpMyAdmin for the database in question.
SET foreign_key_checks = 0;
DROP TABLE civicrm_acl;
... all of the problem tables with their constraints...
DROP TABLE civicrm_worldregion;
SET foreign_key_checks = 1;
(the latter SET is good housekeeping)
Then, I was able to do my import. When I looked at my MySQL export, it had added the constraints at the end of the import, after the data is in place. If your MySQL import puts the constraints in before the data is in place, that will prevent the import from working completely.
Do not import via Phpmyadmin. It usually works really bad. Use the command line.
This should work:
cat your_mysql_dump_file.sql | mysql -uyour_user -p myDB_NAME
I'm using phpMyAdmin, and I'm attempting to duplicate the database from one database to another (on my dev machine). I get the following error:
#1452 - Cannot add or update a child row: a foreign key constraint fails
Behind the scenes, PMA appears to be doing a INSERT INTO ... SELECT query.
Is there a way to get PMA to copy this database properly? Or, is there a better tool for working with MySQL that I should be using (preferably GUI based)?
Export the original DB, then import it to new DB - the SQL created in the export will have a "disable keys" directive, that will lift you over the hurdle.