I have database project migration from Symfony 3.2 -> 3.4
I made new project and copy entity to the new 3.4 environment.
then,
php bin/console doctrine:schema:update --force
it shows the error.
An exception occurred while executing 'DROP INDEX IDX_EFE42E9BC5E35EFC ON placeinfos_placecats':
SQLSTATE[HY000]: General error: 1553 Cannot drop index 'IDX_EFE42E9BC5E35EFC': needed in a foreign key constraint
So, I am checking the placeinfos_placecats table,
mysql> show create table placeinfos_placecats;
| placeinfos_placecats | CREATE TABLE `placeinfos_placecats` (
`placeinfo_id` int(11) NOT NULL,
`placecat_id` int(11) NOT NULL,
PRIMARY KEY (`placeinfo_id`,`placecat_id`),
KEY `IDX_EFE42E9BC5E35EFC` (`placecat_id`),
CONSTRAINT `FK_EFE42E9B6762112` FOREIGN KEY (`placeinfo_id`) REFERENCES `PlaceInfo` (`id`) ON DELETE CASCADE,
CONSTRAINT `FK_EFE42E9BC5E35EFC` FOREIGN KEY (`placecat_id`) REFERENCES `PlaceCat` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
There is constraint IDX_EFE42E9BC5E35EFC.
And then, what the doctrine tries to do,
php bin/console doctrine:schema:update --dump-sql
DROP INDEX IDX_EFE42E9BC5E35EFC ON placeinfos_placecats;
DROP INDEX IDX_EFE42E9B6762112 ON placeinfos_placecats;
ALTER TABLE placeinfos_placecats DROP PRIMARY KEY;
ALTER TABLE placeinfos_placecats ADD place_info_id INT NOT NULL, ADD place_cat_id INT NOT NULL, DROP placeinfo_id, DROP placecat_id;
ALTER TABLE placeinfos_placecats ADD CONSTRAINT FK_EFE42E9B6947D603 FOREIGN KEY (place_info_id) REFERENCES place_info (id) ON DELETE CASCADE;
ALTER TABLE placeinfos_placecats ADD CONSTRAINT FK_EFE42E9B4463AE2A FOREIGN KEY (place_cat_id) REFERENCES place_cat (id) ON DELETE CASCADE;
CREATE INDEX IDX_EFE42E9B6947D603 ON placeinfos_placecats (place_info_id);
CREATE INDEX IDX_EFE42E9B4463AE2A ON placeinfos_placecats (place_cat_id);
ALTER TABLE placeinfos_placecats ADD PRIMARY KEY (place_info_id, place_cat_id);
ALTER TABLE fos_user DROP FOREIGN KEY FK_957A6479B6E0899D;
ALTER TABLE fos_user CHANGE userkey user_key VARCHAR(255) DEFAULT NULL;
ALTER TABLE fos_user ADD CONSTRAINT FK_957A6479B6E0899D FOREIGN KEY (metainfo_id) REFERENCES meta_info (id);
ALTER TABLE media__gallery_media DROP FOREIGN KEY FK_80D4C5414E7AF8F;
ALTER TABLE media__gallery_media DROP FOREIGN KEY FK_80D4C541EA9FDD75;
ALTER TABLE media__gallery_media ADD CONSTRAINT FK_80D4C5414E7AF8F FOREIGN KEY (gallery_id) REFERENCES media__gallery (id) ON DELETE CASCADE;
ALTER TABLE media__gallery_media ADD CONSTRAINT FK_80D4C541EA9FDD75 FOREIGN KEY (media_id) REFERENCES media__media (id) ON DELETE CASCADE;
DROP INDEX IDX_5C6DD74E12469DE2 ON media__media;
ALTER TABLE media__media DROP category_id;
What does it mean???
Just dropping and adding constraint again??
Why doctrine wants to do like this ?
Then ,how should I solve it??
I just deleted the table manually via adminer. Withot "foreign key check".
Then, I did bin/console doctrine:schema:update -f and its ok!
I suppose this closely connected with Foreign Key restrictions. So, you can somehow exclude this check in doctrine.
But be careful. I guess method below isn't really good way for prod.
Actually, you could delete a special KEYNAME via adminer!
IDX_EFE42E9BC5E35EFC, for example...
Related
I have this MySql database table:
tbl_project
id
project_name
parent_id
If a project is a 'parent', parent_id is 0.
Now I'm trying to add a self referencing foreign key
CONSTRAINT `FK_tbl_project_tbl_project` FOREIGN KEY (`parent_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE
When I try to insert a new record,
SQL Error (1452): Cannot add or update a child row: a foreign key constraint fails (mydbname.#sql-3539_d7d, CONSTRAINT FK_tbl_project_tbl_project FOREIGN KEY (parent_id) REFERENCES tbl_project (id) ON DELETE CASCADE)
Basically I just want all the children to be deleted when I delete the parent. What am I missing here?
When you enter any value into the parent_id column of a new row, the foreign key requires that there is a row where id has that value, because that's how foreign keys work.
If you want to use a foreign key with your self-referencing relationship, you should use NULL instead of 0 for the parent_id of a row that has no parent to reference. Foreign keys ignore NULL.
I have a database that i want to do some reverse engineering to tables using doctrine 2, so i used this simple commands
php app/console doctrine:mapping:import --force ProjectBundle yml
php app/console doctrine:mapping:convert annotation ./src
php app/console doctrine:generate:entities ProjectBundle
but the problem doctrine did not reverse engineered one join table containing two foreign keys but it did for another.
here you will find an sql statements for creating the two join tables the first have been reverse engineered the last one not
CREATE TABLE IF NOT EXISTS `control`.`project_problem_place` (
`id_place` INT(11) NOT NULL,
`id_problem` INT(11) NOT NULL,
`id_project` INT(11) NOT NULL,
PRIMARY KEY (`id_place`, `id_problem`, `id_project`),
INDEX `FK_project1` (`id_project` ASC),
INDEX `FK_problem1` (`id_problem` ASC),
CONSTRAINT `FK_problem1`
FOREIGN KEY (`id_problem`)
REFERENCES `control`.`problem` (`id`),
CONSTRAINT `FK_project1`
FOREIGN KEY (`id_project`)
REFERENCES `control`.`project` (`id`),
CONSTRAINT `FK_place1`
FOREIGN KEY (`id_place`)
REFERENCES `control`.`place` (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `control`.`project_code` (
`id_project` INT(11) NOT NULL,
`id_code` INT(11) NOT NULL,
PRIMARY KEY (`id_project`, `id_code`),
INDEX `FK_CODE2` (`id_code` ASC),
CONSTRAINT `FK_PROJECT2`
FOREIGN KEY (`id_project`)
REFERENCES `control`.`project` (`id`),
CONSTRAINT `FK_CODE2`
FOREIGN KEY (`id_code`)
REFERENCES `control`.`code` (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
Thank you.
$ php app/console doctrine:schema:update --force
Updating database schema...
[Doctrine\DBAL\DBALException]
An exception occurred while executing 'ALTER TABLE my_data_and_attribute
ADD CONSTRAINT FK_44E5AE29CAA2B25 FOREIGN KEY (data_id) REFEREN
CES my_data (id)':
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update
a child row: a foreign key constraint fails (`symfony`.`#sql-3e2_186`, CONSTR
AINT `FK_44E5AE29CAA2B25` FOREIGN KEY (`data_id`) REFERENCES `my_data` (`id`))
An error has occurred.
I had recently replace data from other database that had execute update command.
So, I think the cause of error is that I had done replace data.
(I don't know what it may cause from completely different something.)
And I also try to change relationship from phpMyAdmin.
But it failed like this.
Error
SQL query:
ALTER TABLE `my_data_and_attribute` ADD FOREIGN KEY ( `data_id` ) REFERENCES `symfony`.`my_data` (
`id`
) ON DELETE CASCADE ON UPDATE RESTRICT ;
MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`symfony`.`#sql-3e9_1ea`, CONSTRAINT `#sql-3e9_1ea_ibfk_2` FOREIGN KEY (`data_id`) REFERENCES `my_data` (`id`) ON DELETE CASCADE)
I can't understand this error messages.
Where should I check for?
symfony.my_data
# Column Type Collation Attributes Null Default Extra
1 id int(11) No None AUTO_INCREMENT
// ...
Indexes:
Keyname Type Unique Packed Column Cardinality Collation Null Comment
PRIMARY BTREE Yes No id 517 A
symfony.my_data_and_attribute
# Column Type Collation Attributes Null Default Extra
1 data_id int(11) No None
2 attribute_id int(11) No None
INDEXES
Keyname Type Unique Packed Column Cardinality Collation Null Comment
PRIMARY BTREE Yes No data_id 483 A
attribute_id483 A
data_id BTREE No No data_id 483 A
attribute_id BTREE No No attribute_id37 A
Relation view
Column Internal relation Foreign key constraint (INNODB)
data_id // <- I can not set value this point.
attribute_id `symfony`.`my_attribute`.`id` ON DELETE CASCADE ON UPDATE RESTRICT
I would probably need both create table statement to answer correctly, but it's very likely that my_data_and_attribute.data_id is not exactly the same field type than my_data.id .
It can happen for instance if one of the field is UNSIGNED and the other is not or if the fields are not both the same size.
so i am trying again and again to make a column as a foreign key but it gives me Constraint name error? what Constraint name should i put? i am using PHPmyadmin SQL , I tried giving it names such as trackid and so on but still i am getting an error
ALTER TABLE `ss_ordered_carts`
ADD CONSTRAINT `track_id` FOREIGN KEY (`track_id`)
REFERENCES `ProjectDatabase`.`ss_orders` (`track_id`)
ON DELETE RESTRICT ON UPDATE CASCADE;
MySQL said: Documentation
1452 - Cannot add or update a child row: a foreign key constraint fails (projectdatabase.
'#sql-1414_6c7'>, CONSTRAINT track_id FOREIGN KEY (track_id) REF
ERENCES ss_orders (track_id) ON UPDATE CASCADE)
If you have data in child table for the referred column field, to add foreign key on that field,
first you have to disable foreign key checking.
And then alter to add foreign key.
And lastly reset the constraint checking.
Step 1: Disable the foreign key checks.
set foreign_key_checks=0;
Step 2: Now add the foreign key constraint.
ALTER TABLE `ss_ordered_carts`
ADD CONSTRAINT `track_id` FOREIGN KEY (`track_id`)
REFERENCES `ProjectDatabase`.`ss_orders` (`track_id`)
ON DELETE RESTRICT ON UPDATE CASCADE;
Step 3: Now reset the foreign key checks.
set foreign_key_checks=1;
It is possible that you have table ss_ordered_carts filled with wrong data, that cannot be related. Check it using this query -
SELECT * FROM ss_ordered_carts t1
LEFT JOIN ss_orders t2
ON t1.track_id = t2.track_id
WHERE
t2.track_id IS NULL;
Then modify table's data, and after, create foreign key.
Try this.
mysql> SET foreign_key_checks = 0;
mysql> ALTER TABLE ss_ordered_carts
ADD CONSTRAINT track_id
FOREIGN KEY(track_id)
REFERENCES ss_orders(track_id)
mysql> SET foreign_key_checks = 1;
May be, you are getting the specified error as there exists data in the tables.
This may also occur if child table contain some data with the foreign key that that are not in parent table. In this case try,
Delete from child where child_id NOT IN (select parent_id from parent where parent.id = child.id)
I needed an example of cascade delete. My question is, where do I add it? In the table I create PK, or in other tables where this PK is sitting there as FK?
Can I use "alter table" to add "on delete cascade" to existing table? An example please?
#edit
MYSQL, using phpMyAdmin
#edit 2
Does it look good?
alter table wplaty
drop foreign key pesel,
add constraint pesel foreign key (pesel)
references baza_osob(pesel) on delete cascade on update restrict;
My parent table = baza_osob
My child table = wplaty
PK is pesel, FK is pesel as well.
#edit3
Got error:
#1025 - Error on rename of '.\projekt\wplaty' to '.\projekt#sql2-1300-6c' (errno: 152)
cascade directives go into the "child" tables, e.g.
create table parent (
id int primary key
)
create table child (
id int primary key
parent_id int,
foreign key (parent_id) references parent (id)
on delete cascade
)
Never tried doing an alter on a foreign key to change its on settings, but worst case, you simply drop the existing FK and redefine it with the new on settings in place.
You actually don't add the 'ON DELETE CASCADE' on a table. It's part (or not) of each Foreign Key definition.
You need to enable this option for the foreign key. If you did not add this option when you created foreign key, then you should recreate it.
alter table <table> drop foreign key <fk name>;
alter table <table> add constraint <fk name> foreign key (<column name>)
references <ref tble>(<ref column) on delete cascade on update restrict;
In one statement:
alter table <table>
drop foreign key <old fk name>,
add constraint <new fk name> foreign key (<column name>)
references <ref tble>(<ref column) on delete cascade on update restrict;