Database migration, read constraint - yii2

I need to migrate a database in yii2. I want to read from MySQL database values for foreign key constraints, on delete cascade, restrict, set null. The same for on update values. Is it some possiblities to do it in migration tool. I use this tool: https://github.com/mootensai/yii2-enhanced-gii.

Try to use getSchema()->getTableSchema('table_name')->foreignKeys on your DB connection.

Related

Whenever I try to set foreign key constraint to "NO ACTION" MySQL workbench automatically sets it to "RESTRICT"

I am trying to set a foreign key to constraint for on delete and on update to "NO ACTION", but for some reason after I apply the changes MySQL workbench changes it back to "RESTRICTED" on its own, I don't know why it's doing that.
This is the code MySQL Workbench generates when I try to change the constraints for on update and on delete to "NO ACTION"
ALTER TABLE `forums`.`post_replies`
DROP FOREIGN KEY `fk_post_replies_users`;
ALTER TABLE `forums`.`post_replies`
ADD CONSTRAINT `fk_post_replies_users`
FOREIGN KEY (`user_id`)
REFERENCES `forums`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
NO ACTION and RESTRICT are synonyms.
"13.1.20.5 FOREIGN KEY Constraints":
NO ACTION: A keyword from standard SQL. In MySQL, equivalent to RESTRICT.
Workbench seems to just pick to display RESTRICT over NO ACTION (well, it has to chose one and chooses the MySQL specific one...). But it doesn't mean anything different.

SQLAlchemy configration for MySQL's "SET foreign_key_checks = 0"

I am using SQLAlchemy with Flask to create database tables - every table has at least one foreign key - it works with sqlite but not MySQL - I get foreign key integrity error when creating the tables in MySQL (the parent table is not created when creating the child table). I use "SET foreign_key_checks = 0" to solve the problem but that does not work with sqlite. Is there a way to configure SQLAlchemy to ignore foreign key checks?
if you are user mysql, you can connect to mysql and use SET GLOBAL FOREIGN_KEY_CHECKS = 0; delete the db table you want, and again SET GLOBAL FOREIGN_KEY_CHECKS = 1;. This value verifies foreign relationships in the db tables.
First of all, why would you want to ignore foreign key constraints? When you define your foreign key you can pass a string with the foreign table and column names, and those will be resolved correctly even if the foreign table hasn't been created yet.
But in any case, if you want to have foreign keys that are not enforced, just don't define your foreign key columns as foreign keys, define them as simple columns and manage the foreign key dependency yourself. Not a good idea, in my opinion, but it can be done.

Names of foreign key constraints changed in mysql dump

Whenever i make a database backup using this command:
mysqldump --all-databases
It will change all the foreign key constraints names. We are using Syfmony2 framework with doctrine migrations. It looks like mysql apllies a naming strategy for foreign keys. I wonder if it's possible too keep the foreign key constraints names the same.
Any ideas?
For example: on the constraint for user is FK_B2D59BB34A8BC2 In the backup it says User_ibfk_1
Thanks.

Foreign Key Constraints - DB won't import. - Magento

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

Grails GORM and MYSQL cascade delete problem

I have a table User that has many child tables defined in User class under the static hasMany
grails.
I have no problem when doing User.get(3).delete() in grails. It automatically delete that user and all its child table rows.
But when I want to perform the same operation in MySQL workbench. I get Error thrown by MySQL:
ERROR 1451: Cannot delete or update a parent row: a foreign key constraint fails (`test_db`.`search_stat`, CONSTRAINT `FK7A3CFFB6E64DB41` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))
SQL Statement:
DELETE FROM `test_db`.`user` WHERE `id`='3'
I dont know what is the problem here with MySQL.
Where did you read about the "ON DELETE CASCADE" in the documentation? I haven't found it here. The poster of this post had to manually add this as well to get the desired behaviour.
If grails is really supposed to add this, have you tried deleting the DB and having it re-created by grails (at least in development environment)? Maybe the current schema was generated before you added the belongsTo or something?
Also check out this Blog post about GORM Gotchas regarding hasMany and belongsTo.