Copy Database using phpMyAdmin fails due to Foreign Key Constraints - mysql

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.

Related

MySQL not deleting referential constraint on drop of database

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)

Migrating from MariaDB to Mysql - Duplicate Constraint Name

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.

Aurora MySQL - failed to delete a record

Today I failed to delete a record from a table in my database. I tried on both of my database management applications in MAC: MySQL Pro and MySQLWorkbench, but each of them created different error!
MySQL Pro
MySQLWorkbench
For both MySQL Pro and MySQLWorkbench, I am sure that I am log in using primary instance's link (not replica's) with user having DELETE privilege on this database.
Why the same database responds 2 different errors?
What settings should I look at in order to fix this?
I have checked all foreign keys referring to the record ID which I currently want to delete. It looks good.
Then I delete all records in the "child" tables referring to the ID of the record; and go back to delete the record -> it works!
I remember I had once changed a foreign keys from CASCADE for ON DELETE/UPDATE to NULL for ON DELETE/UPDATE, then I rolled back (CASCADE for ON DELETE/UPDATE). Not sure if that is the root cause.
Anyhow, I get my job done and I'd like to confirm that I have been working with the primary Aurora instance since the beginning, not replica.

Loading data from Json file into PostgresSql causes: ERROR "duplicate key value violates unique constraint - already exists."

I am deploying my Django site on to Google Cloud. One of the step is to change the database to postgresSQL. As I am using SqlLite locally I wanted to migrate all of the database into postgresSql. I followed an online guide where you Dump your data first and then change Database in settings.py to your new database. I have done everything upto this command;
python manage.py loaddata datadump.json
where datadump.json is the dumped database from SQLITE. Now I am stuck with this error
django.db.utils.IntegrityError: Problem installing fixtur, Could not
load users.Profile(pk=3): duplicate key value violates unique
constraint "users_profile_user_id_key" DETAIL: Key (user_id)=(1) already exists.
and I don't have any idea as to what to do. Some answers I looked up such as this:
postgresql duplicate key violates unique constraint
AND
Django admin "duplicate key value violates unique constraint" Key (user_id)=(1) already exists
haven't helped, as I cannot understand what's going on. I did use MySQL 6 years ago, but I cannot understand this.
I managed to run some SQL commands from online resources and managed to produce this for my database:
!https://imgur.com/a/qQNLEs7
I followed these guides:
https://medium.com/#aaditya.chhabra/how-to-use-postgresql-with-your-django-272d59d28fa5
https://www.shubhamdipt.com/blog/django-transfer-data-from-sqlite-to-another-database/
Drop the database
create a new one and run migrations
python manage.py migrate
And try importing data from json file using,
python manage.py loaddata datadump.json
The errors are caused by the superuser object in the database you created using 'createsuperuser' command.
TL;DR
Just delete the unwanted record from django_site table.
Brief
I had the same problem because I used Signin with Google in my site. So I tried it 2 times to see how to setup Social Login. As a result, table django_site created 2 records. Just delete the unwanted record from that Table or Collection

Link in foreign keys in phpmyadmin

I have foreign keys set in a mysql db. I have backed up my db from one server to a new one. Both the new and the old server use phpmyadmin. On the old server, y used to have a link in the view of a table from the foreign key to the respective register in the other table. An html link that leads me to the register in the other table. But it doesn't appear on the new server.
Any ideas?
You'll need to set up the pmadb meta-tables: http://www.phpmyadmin.net/documentation/#linked-tables
After that, you should get the links between foreign keys when browsing. You can also use $cfg['Servers'][$i]['relation'] to set up the relations you want hyperlinked by hand: http://www.phpmyadmin.net/documentation/#cfg_Servers_relation
I don't really get what you mean. In order to import data to a new phpMyAdmin, you need to export the data to sql and import the exported sql file. All the foreign key will still remain the same as what you have imported is in the form of sql.