Tortoise ORM: how to disable foreign key checks temporarily? - mysql

In raw MySQL it's easy to disable foreign key checks temporarily while you're doing migrations etc.
SET foreign_key_checks = 0;
My team uses Tortoise ORM and we need to replicate that functionality. How can we do that?
Google and Stack Overflow searches have yielded nothing.
Migrations generated by Tortoise ORM fail when a FK constraint needs to be ignored.

Related

How does Schema::disableForeignKeyConstraints in Laravel work for MySQL?

When performing actions that requires foreign key contraints to be disabled, one can temporarily disable foreign key constraints to perform such an action, eg:
Schema::disableForeignKeyConstraints();
// Do good stuff
Schema::enableForeignKeyConstraints();
This made me think if the restraints are disabled in the schema for all connections?
Is it possible for another connection in another process to break the restraint between the disabling and enabling?
I've tried to test this by running Schema::disableForeignKeyConstraints(); in Tinker, and opening a new terminal to attempt to break some foreign key constraints - but they still apply, and I'm not sure why.
The Schema builder uses
public function disableForeignKeyConstraints()
{
return $this->connection->statement(
$this->grammar->compileDisableForeignKeyConstraints()
);
}
and for MySQL, the compileDisableForeignKeyConstraints function is defined as
public function compileDisableForeignKeyConstraints()
{
return 'SET FOREIGN_KEY_CHECKS=0;';
}
so it seems that running Schema::disableForeignKeyConstraints(); should simply disable the foreign key restraints for the entire schema - yet it seems to only work on a per-connection basis.
Is this a Laravel thing or a MySQL thing? How does it work?

How to disable foreign key checks while migrating to google cloud sql due to circular references

I'm trying to migrate a production database to google cloud sql using the replication method described here. However, when importing the mysql dump file google cloud sql returns the error ''Cannot Add Foreign Key Constraint''.
I've checked if there are any foreign key vialotions using this method. However, there are no violations in the database.
Therefore, I've tracked down the problem to be due to circular references. The only option seems to be to disable FOREIGN_KEY_CHECKS during the import. However, I cannot find any method to set this flag.
Are there any recommendations on how to continue from here?
SET FOREIGN_KEY_CHECKS := NOT ##FOREIGN_KEY_CHECKS
;
Will toggle FK control in mysql.

How to disable all foreign keys in phpMyAdmin when exporting table data?

I exported all tables of my database. I opened the generated file then I saw that phpMyAdmin sorts the queries by table_name. So there is potential foreign key issues because master table is created/inserted after detail table! So how to disable foreign key checks when exporting table data with phpMyAdmin, and how to re-enable them at the end of the script?
Just disable foreign key checks before and re-enable them after you execute your script:
SET foreign_key_checks = 0;
-- run some queries
SET foreign_key_checks = 1;
From the Export tab, select the "Custom" export method.
In the "Format-specific options:" area, look for and check "Disable foreign key checks".
It does the same thing Misa Lazovic suggested to do, but since you're using phpMyAdmin to do the export this is the graphical way of adding those lines.
I also faced the same issue for importing the database in server. And tried all answers above but couldn't figure out. This tutorial fixed my issue and made me able to import my DB in phpmyadmin. Tutorial
Hope this one helped!
When creating the foreign key you should add this option :
ON DELETE CASCADE Or
On delete set null
So when you will delete the foreign key, there will be no PB.

Creating foreign keys using hibernate

I encounter the following problem with Hibernate and foreign keys:
When I first deploy my web application, Hibernate was configured with this parameters (among many others):
databasePlatform set to "generic" (not engine specific) dialect org.hibernate.dialect.MySQLDialect.
hibernate.hbm2ddl.auto set to update
As the default engine was MyISAM, Hibernate logically created MyISAM tables with indexes, ignoring creation foreign keys (since MyISAM doesn't support such constraints).
Now that I want to migrate every tables to InnoDB, I would like Hibernate to automatically create missing foreign keys. Unfortunately, it looks like Hibernate is just looking for the index :
If the index exists, Hibernate will not create the corresponding foreign key;
If I drop the index, Hibernate will create both index and foreign key.
Since I don't want to drop every index in my schema, do you know a way to tell Hibernate to create the foreign key even if the index is created?
Thank you.
My suggestion would be to allow hibernate to recreate the entire schema since this is your development database.
If however, you have lots of test data you don't want to lose then I would create a script using the available MySQL commands to automatically drop all of your indexes for Hibernate to recreate them. The commands you will need are:
show tables;
show index from `table_name`;
drop index `index_name` on `table_name`;
Organise these into some form of program script (shell, python or the application your building etc.) and you're good to go.

foreign key constraint not respected

I have recently switched jobs and at this new company we are using MySQL. I don't have any expereince with MySQL, although I have used SQL Server and Oracle for over 4 years now.
Now the strange thing I see with MySQL is that it does not seem to resepect some of the basic things like Foreign Key Constraints (meaning a column is a foregin key but i can insert any value here no matter if it's present in the other table where this FK related to). Now I know in SQL Server there is this concept of a NOCHECK foriegn key constraint but the guy at new company responsible for MySQL db say that not respecting a FK is a normal thing in MySQL and it does not need to have any special settings (like NOCHECK FK constraint).
I fail to understand that in a database system how can you ensure referential integirty without having these basic checks in place. I am not sure if the local mySQL "expert" know it well or it's just that mySQL really does not respect FK rules. Any thoughts?
Check that your tables are using the InnoDB engine. When using the MyISAM engine (which was the default until recently), foreign keys declarations are not enforced.
MySQL have different DB Engines -
MyISAM - default, no FK support
InnoDB - have FK support - but no fulltext search like in MyISAM
On both engines you can create table and try to create FK, but MyISAM will simply ignore it.
Also, make sure foreign keys are being enforced. For some reason they weren't on mine, leading to one week of headache!
Check:
SELECT ##FOREIGN_KEY_CHECKS
Set:
SET FOREIGN_KEY_CHECKS=1