How to drop auto_increment from a mysql table - mysql

this should be a very easy issue but I couldn't find a solution that works.
I migrate the date from Oracle to MYSQL and during the process, all primary keys were set to auto_increment.
However, there are a lot of identified relationships (parent PK is the same of children).
So the correct way to do the transaction is to insert into the parent tables, get result.insertId from this interaction and then insert the same value in the child table. I know that I could simply ignore the auto_increment sending the id in the insert command but I didn't like to just let this go.
As the solutions I read about say that I need to change the column to the exactly the same specification but auto_increment, I run the following SQL:
alter table added_object modify column id_interaction_object int(11) not null;
.. And I get the following message:
ERROR 1833 (HY000): Cannot change column 'id_interaction_object': used
in a foreign key constraint 'FK__METRIC__ADDED_OBJECT' of table
'metric'
Any tips?
Thanks

You need to disable foreign key checks:
SET FOREIGN_KEY_CHECKS=0;
alter table added_object modify column id_interaction_object int(11) not null;
SET FOREIGN_KEY_CHECKS=1;

Related

what is wrong with this foreign key constraints [duplicate]

I have two tables, table1 is the parent table with a column ID and table2 with a column IDFromTable1 (not the actual name) when I put a FK on IDFromTable1 to ID in table1 I get the error Foreign key constraint is incorrectly formed error. I would like to delete table 2 record if table1 record gets deleted. Thanks for any help
ALTER TABLE `table2`
ADD CONSTRAINT `FK1`
FOREIGN KEY (`IDFromTable1`) REFERENCES `table1` (`ID`)
ON UPDATE CASCADE
ON DELETE CASCADE;
Let me know if any other information is needed. I am new to mysql
I ran into this same problem with HeidiSQL. The error you receive is very cryptic. My problem ended up being that the foreign key column and the referencing column were not of the same type or length.
The foreign key column was SMALLINT(5) UNSIGNED and the referenced column was INT(10) UNSIGNED. Once I made them both the same exact type, the foreign key creation worked perfectly.
For anyone facing this problem, just run
SHOW ENGINE INNODB STATUS
and see the LATEST FOREIGN KEY ERROR section for details.
I had the same problem when the parent table was created using MyISAM engine. It's a silly mistake, which I fixed with:
ALTER TABLE parent_table ENGINE=InnoDB;
make sure columns are identical(of same type) and if reference column is not primary_key, make sure it is INDEXED.
Syntax for defining foreign keys is very forgiving, but for anyone else tripping up on this, the fact that foreign keys must be "of the same type" applies even to collation, not just data type and length and bit signing.
Not that you'd mix collation in your model (would you?) but if you do, be sure your primary and foreign key fields are of the same collation type in phpmyadmin or Heidi SQL or whatever you use.
Hope this saves you the four hours of trial and error it cost me.
I had same problem, but solved it.
Just make sure that column 'ID' in 'table1' has UNIQUE index!
And of course the type, length of columns 'ID' and 'IDFromTable1' in these two tables has to be same. But you already know about this.
mysql error texts doesn't help so much, in my case, the column had "not null" constraint, so the "on delete set null" was not allowed
Just for completion.
This error might be as well the case if you have a foreign key with VARCHAR(..) and the charset of the referenced table is different from the table referencing it.
e.g. VARCHAR(50) in a Latin1 Table is different than the VARCHAR(50) in a UTF8 Table.
One more probable cause for the display of this error. The order in which I was creating tables was wrong. I was trying to reference a key from a table that was not yet created.
I had the same issue, both columns were INT(11) NOT NULL but I wan't able to create the foreign key.
I had to disable foreign keys checks to run it successfully :
SET FOREIGN_KEY_CHECKS=OFF;
ALTER TABLE ... ADD CONSTRAINT ...
SET FOREIGN_KEY_CHECKS=ON;
Hope this helps someone.
if everything is ok, just add ->unsigned(); at the end of foregin key.
if it does not work, check the datatype of both fields. they must be the same.
(Last Resent) Even if the field name and data type is the same but the collation is not the same, it will also result to that problem.
For Example
TBL
NAME | DATA
TYPE |
COLLATION
ActivityID | INT |
latin1_general_ci
ActivityID | INT |
utf8_general_ci
Try Changing it into
TBL
NAME | DATA
TYPE |
COLLATION
ActivityID | INT |
latin1_general_ci
ActivityID | INT |
latin1_general_ci
....
This worked for me.
This problem also occur in Laravel when you have the foreign key table table1 migration after the migration in which you reference it table2.
You have to preserve the order of the migration in order to foreign key feature to work properly.
database/migrations/2020_01_01_00001_create_table2_table.php
database/migrations/2020_01_01_00002_create_table1_table.php
should be:
database/migrations/2020_01_01_00001_create_table1_table.php
database/migrations/2020_01_01_00002_create_table2_table.php
Check the tables engine, both tables have to be the same engine, that helped me so much.
Although the other answers are quite helpful, just wanted to share my experience as well.
I faced the issue when I had deleted a table whose id was already being referenced as foreign key in other tables (with data) and tried to recreate/import the table with some additional columns.
The query for recreation (generated in phpMyAdmin) looked like the following:
CREATE TABLE `the_table` (
`id` int(11) NOT NULL, /* No PRIMARY KEY index */
`name` varchar(255) NOT NULL,
`name_fa` varchar(255) NOT NULL,
`name_pa` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
... /* SOME DATA DUMP OPERATION */
ALTER TABLE `the_table`
ADD PRIMARY KEY (`id`), /* PRIMARY KEY INDEX */
ADD UNIQUE KEY `uk_acu_donor_name` (`name`);
As you may notice, the PRIMARY KEY index was set after the creation (and insertion of data) which was causing the problem.
Solution
The solution was to add the PRIMARY KEY index on table definition query for the id which was being referenced as foreign key, while also removing it from the ALTER TABLE part where indexes were being set:
CREATE TABLE `the_table` (
`id` int(11) NOT NULL PRIMARY KEY, /* <<== PRIMARY KEY INDEX ON CREATION */
`name` varchar(255) NOT NULL,
`name_fa` varchar(255) NOT NULL,
`name_pa` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Try running following:
show create table Parent
//and check if type for both tables are the same, like myISAM or innoDB, etc
//Other aspects to check with this error message: the columns used as foreign
keys must be indexed, they must be of the same type
(if i.e one is of type smallint(5) and the other of type smallint(6),
it won't work), and, if they are integers, they should be unsigned.
//or check for charsets
show variables like "character_set_database";
show variables like "collation_database";
//edited: try something like this
ALTER TABLE table2
ADD CONSTRAINT fk_IdTable2
FOREIGN KEY (Table1_Id)
REFERENCES Table1(Table1_Id)
ON UPDATE CASCADE
ON DELETE CASCADE;
I lost for hours for that!
PK in one table was utf8 in other was utf8_unicode_ci!
thanks S Doerin:
"Just for completion.
This error might be as well the case if you have a foreign key with VARCHAR(..) and the charset of the referenced table is different from the table referencing it.
e.g. VARCHAR(50) in a Latin1 Table is different than the VARCHAR(50) in a UTF8 Table."
i solved this problem, changing the type of characters of the table.
the creation have latin1 and the correct is utf8.
add the next line.
DEFAULT CHARACTER SET = utf8;
I had the same problems.
The issue is the reference column is not a primary key.
Make it a primary key and problem is solved.
My case was that I had a typo on the referred column:
MariaDB [blog]> alter table t_user add FOREIGN KEY ( country_code ) REFERENCES t_country ( coutry_code );
ERROR 1005 (HY000): Can't create table `blog`.`t_user` (errno: 150 "Foreign key constraint is incorrectly formed")
The error message is quite cryptic and I've tried everything - verifying the types of the columns, collations, engines, etc.
It took me awhile to note the typo and after fixing it all worked fine:
MariaDB [blog]> alter table t_user add FOREIGN KEY ( country_code ) REFERENCES t_country ( country_code );
Query OK, 2 rows affected (0.039 sec)
Records: 2 Duplicates: 0 Warnings: 0
I face this problem the error came when you put the primary key in different data type like:
table 1:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('product_name');
});
table 2:
Schema::create('brands', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('brand_name');
});
the data type for id of the second table must be increments
For anyone struggling as I was with this issue, this was my problem:
I was trying to alter a table to change a field from VARCHAR(16) to VARCHAR(255) and this was referencing another table column where the datatype was still VARCHAR(16)...
I had the same issue with Symfony 2.8.
I didn't get it at first, because there were no similar problems with int length of foreign keys etc.
Finally I had to do the following in the project folder. (A server restart didn't help!)
app/console doctrine:cache:clear-metadata
app/console doctrine:cache:clear-query
app/console doctrine:cache:clear-result
I was using HeidiSQL and to solve this problem I had to create an index in the referenced table with all the columns being referenced.
I had issues using Alter table to add a foreign key between two tables and the thing that helped me was making sure each column that I was trying to add a foreign key relationship to was indexed. To do this in PHP myAdmin:
Go to the table and click on the structure tab.
Click the index option to index the desired column as shown in screenshot:
Once I indexed both columns I was trying to reference with my foreign keys, I was able to successfully use the alter table and create the foreign key relationship. You will see that the columns are indexed like in the below screenshot:
notice how zip_code shows up in both tables.
I ran into the same issue just now. In my case, all I had to do is to make sure that the table I am referencing in the foreign key must be created prior to the current table (earlier in the code). So if you are referencing a variable (x*5) the system should know what x is (x must be declared in earlier lines of code). This resolved my issue, hope it'll help someone else.
The problem is very simple to solve
e.g: you have two table with names users and posts and you want create foreign key in posts table and you use phpMyAdmin
1) in post table add new column
(name:use_id | type: like the id in user table | Length:like the id in user table | Default:NULL | Attributes:unsigned | index:INDEX )
2)on Structure tab go to relation view
(Constraint name: auto set by phpmyAdmin | column name:select user_id |table:users | key: id ,...)
It was simply solved
javad mosavi iran/urmia
I had the same error, and I discovered that on my own case, one table was MyISAM, and the other one INNO. Once I switched the MyISAM table to INNO. It solved the issue.
One more solution which I was missing here is, that each primary key of the referenced table should have an entry with a foreign key in the table where the constraint is created.
If U Table Is Myisum And New Table Is InoDb you Are Note Foreign
You Must Change MyIsum Table To InoDb

Can't alter table to remove auto_increment due to foreign keys

I'm trying to remove Auto_Increment from the column _id in my MySQL database. However that column is the primary key for the table and when I'm using this command
ALTER TABLE Profile
MODIFY _id INT PRIMARY KEY NOT NULL
I get an error telling me that I can't do that since there are other tables which references the primary key.
My question is therefore: Is there a way to get around this problem?
the easiest and fastest way is the following:
set foreign_key_checks = 0;
alter table Profile change column _id _id INT NOT NULL;
set foreign_key_checks = 1;
found here
Thre options:
1.Delete relationship before making this change.
2.Delete other table/s before making this change.
3.Alter relationship(tables) with somthing like on update / cascade (not sure if this will help)

Lost table on failed removal of MySQL foreign key

I have a table like so:
CREATE TABLE `Words` (
`wordId` int(11) NOT NULL AUTO_INCREMENT,
... snip! ...
`PartOfSpeechpartOfSpeechId` int(11) DEFAULT NULL,
PRIMARY KEY (`wordId`) USING BTREE,
KEY `fk_Words_PartOfSpeech` (`PartOfSpeechpartOfSpeechId`),
CONSTRAINT `fk_Words_PartOfSpeech` FOREIGN KEY (`PartOfSpeechpartOfSpeechId`) REFERENCES `PartOfSpeech` (`partOfSpeechId`) ON DELETE SET NULL ON UPDATE NO ACTION,
) ENGINE=InnoDB AUTO_INCREMENT=7306 DEFAULT CHARSET=utf8
I need to convert the PartOfSpeechpartOfSpeechId column into an enumerated value (ie keep it as an INT column, but remove the table it references entirely).
Trying to drop the PartOfSpeech table fails as one would expect, due to the FK on Words. To fix this, I've run ALTER TABLE Words DROP FOREIGN KEY fk_Words_PartOfSpeech, using the name of the FK instead of the column as noted everywhere. I still get an error:
ERROR 1025 (HY000): Error on rename of './lexercise/#sql-1a5_263' to './lexercise/Words' (errno: 150)
On looking for the table Words, it has vanished. Gone. No idea where to. Presumably, MySQL's internal alter table code is creating a temp table and is failing to return it to its proper name/location.
What is going on here? How is it possible to lose a whole table on dropping a foreign key? My background is in Postgres, not sure how best to debug this.
OK, so for anyone who finds this at some point with the same issue, here's what I think was happening:
I had another foreign key on the table (not shown above) that referenced a table that had been renamed prior to trying to drop the FK above. I think that the presence of that FK was what was causing the error on my ALTER TABLE call.
MySQL basically rebuilds the table schema during ALTER TABLE calls - it does not make incremental changes. As a result, if you have other things wrong with the table schema, make sure you identify them and fix them as well if you're losing tables randomly.
Oh, and MySQL REALLY should handle this better. My god.

mysql Foreign key constraint is incorrectly formed error

I have two tables, table1 is the parent table with a column ID and table2 with a column IDFromTable1 (not the actual name) when I put a FK on IDFromTable1 to ID in table1 I get the error Foreign key constraint is incorrectly formed error. I would like to delete table 2 record if table1 record gets deleted. Thanks for any help
ALTER TABLE `table2`
ADD CONSTRAINT `FK1`
FOREIGN KEY (`IDFromTable1`) REFERENCES `table1` (`ID`)
ON UPDATE CASCADE
ON DELETE CASCADE;
Let me know if any other information is needed. I am new to mysql
I ran into this same problem with HeidiSQL. The error you receive is very cryptic. My problem ended up being that the foreign key column and the referencing column were not of the same type or length.
The foreign key column was SMALLINT(5) UNSIGNED and the referenced column was INT(10) UNSIGNED. Once I made them both the same exact type, the foreign key creation worked perfectly.
For anyone facing this problem, just run
SHOW ENGINE INNODB STATUS
and see the LATEST FOREIGN KEY ERROR section for details.
I had the same problem when the parent table was created using MyISAM engine. It's a silly mistake, which I fixed with:
ALTER TABLE parent_table ENGINE=InnoDB;
make sure columns are identical(of same type) and if reference column is not primary_key, make sure it is INDEXED.
Syntax for defining foreign keys is very forgiving, but for anyone else tripping up on this, the fact that foreign keys must be "of the same type" applies even to collation, not just data type and length and bit signing.
Not that you'd mix collation in your model (would you?) but if you do, be sure your primary and foreign key fields are of the same collation type in phpmyadmin or Heidi SQL or whatever you use.
Hope this saves you the four hours of trial and error it cost me.
I had same problem, but solved it.
Just make sure that column 'ID' in 'table1' has UNIQUE index!
And of course the type, length of columns 'ID' and 'IDFromTable1' in these two tables has to be same. But you already know about this.
mysql error texts doesn't help so much, in my case, the column had "not null" constraint, so the "on delete set null" was not allowed
Just for completion.
This error might be as well the case if you have a foreign key with VARCHAR(..) and the charset of the referenced table is different from the table referencing it.
e.g. VARCHAR(50) in a Latin1 Table is different than the VARCHAR(50) in a UTF8 Table.
One more probable cause for the display of this error. The order in which I was creating tables was wrong. I was trying to reference a key from a table that was not yet created.
I had the same issue, both columns were INT(11) NOT NULL but I wan't able to create the foreign key.
I had to disable foreign keys checks to run it successfully :
SET FOREIGN_KEY_CHECKS=OFF;
ALTER TABLE ... ADD CONSTRAINT ...
SET FOREIGN_KEY_CHECKS=ON;
Hope this helps someone.
if everything is ok, just add ->unsigned(); at the end of foregin key.
if it does not work, check the datatype of both fields. they must be the same.
(Last Resent) Even if the field name and data type is the same but the collation is not the same, it will also result to that problem.
For Example
TBL
NAME | DATA
TYPE |
COLLATION
ActivityID | INT |
latin1_general_ci
ActivityID | INT |
utf8_general_ci
Try Changing it into
TBL
NAME | DATA
TYPE |
COLLATION
ActivityID | INT |
latin1_general_ci
ActivityID | INT |
latin1_general_ci
....
This worked for me.
This problem also occur in Laravel when you have the foreign key table table1 migration after the migration in which you reference it table2.
You have to preserve the order of the migration in order to foreign key feature to work properly.
database/migrations/2020_01_01_00001_create_table2_table.php
database/migrations/2020_01_01_00002_create_table1_table.php
should be:
database/migrations/2020_01_01_00001_create_table1_table.php
database/migrations/2020_01_01_00002_create_table2_table.php
Check the tables engine, both tables have to be the same engine, that helped me so much.
Although the other answers are quite helpful, just wanted to share my experience as well.
I faced the issue when I had deleted a table whose id was already being referenced as foreign key in other tables (with data) and tried to recreate/import the table with some additional columns.
The query for recreation (generated in phpMyAdmin) looked like the following:
CREATE TABLE `the_table` (
`id` int(11) NOT NULL, /* No PRIMARY KEY index */
`name` varchar(255) NOT NULL,
`name_fa` varchar(255) NOT NULL,
`name_pa` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
... /* SOME DATA DUMP OPERATION */
ALTER TABLE `the_table`
ADD PRIMARY KEY (`id`), /* PRIMARY KEY INDEX */
ADD UNIQUE KEY `uk_acu_donor_name` (`name`);
As you may notice, the PRIMARY KEY index was set after the creation (and insertion of data) which was causing the problem.
Solution
The solution was to add the PRIMARY KEY index on table definition query for the id which was being referenced as foreign key, while also removing it from the ALTER TABLE part where indexes were being set:
CREATE TABLE `the_table` (
`id` int(11) NOT NULL PRIMARY KEY, /* <<== PRIMARY KEY INDEX ON CREATION */
`name` varchar(255) NOT NULL,
`name_fa` varchar(255) NOT NULL,
`name_pa` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Try running following:
show create table Parent
//and check if type for both tables are the same, like myISAM or innoDB, etc
//Other aspects to check with this error message: the columns used as foreign
keys must be indexed, they must be of the same type
(if i.e one is of type smallint(5) and the other of type smallint(6),
it won't work), and, if they are integers, they should be unsigned.
//or check for charsets
show variables like "character_set_database";
show variables like "collation_database";
//edited: try something like this
ALTER TABLE table2
ADD CONSTRAINT fk_IdTable2
FOREIGN KEY (Table1_Id)
REFERENCES Table1(Table1_Id)
ON UPDATE CASCADE
ON DELETE CASCADE;
I lost for hours for that!
PK in one table was utf8 in other was utf8_unicode_ci!
thanks S Doerin:
"Just for completion.
This error might be as well the case if you have a foreign key with VARCHAR(..) and the charset of the referenced table is different from the table referencing it.
e.g. VARCHAR(50) in a Latin1 Table is different than the VARCHAR(50) in a UTF8 Table."
i solved this problem, changing the type of characters of the table.
the creation have latin1 and the correct is utf8.
add the next line.
DEFAULT CHARACTER SET = utf8;
I had the same problems.
The issue is the reference column is not a primary key.
Make it a primary key and problem is solved.
My case was that I had a typo on the referred column:
MariaDB [blog]> alter table t_user add FOREIGN KEY ( country_code ) REFERENCES t_country ( coutry_code );
ERROR 1005 (HY000): Can't create table `blog`.`t_user` (errno: 150 "Foreign key constraint is incorrectly formed")
The error message is quite cryptic and I've tried everything - verifying the types of the columns, collations, engines, etc.
It took me awhile to note the typo and after fixing it all worked fine:
MariaDB [blog]> alter table t_user add FOREIGN KEY ( country_code ) REFERENCES t_country ( country_code );
Query OK, 2 rows affected (0.039 sec)
Records: 2 Duplicates: 0 Warnings: 0
I face this problem the error came when you put the primary key in different data type like:
table 1:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('product_name');
});
table 2:
Schema::create('brands', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('brand_name');
});
the data type for id of the second table must be increments
For anyone struggling as I was with this issue, this was my problem:
I was trying to alter a table to change a field from VARCHAR(16) to VARCHAR(255) and this was referencing another table column where the datatype was still VARCHAR(16)...
I had the same issue with Symfony 2.8.
I didn't get it at first, because there were no similar problems with int length of foreign keys etc.
Finally I had to do the following in the project folder. (A server restart didn't help!)
app/console doctrine:cache:clear-metadata
app/console doctrine:cache:clear-query
app/console doctrine:cache:clear-result
I was using HeidiSQL and to solve this problem I had to create an index in the referenced table with all the columns being referenced.
I had issues using Alter table to add a foreign key between two tables and the thing that helped me was making sure each column that I was trying to add a foreign key relationship to was indexed. To do this in PHP myAdmin:
Go to the table and click on the structure tab.
Click the index option to index the desired column as shown in screenshot:
Once I indexed both columns I was trying to reference with my foreign keys, I was able to successfully use the alter table and create the foreign key relationship. You will see that the columns are indexed like in the below screenshot:
notice how zip_code shows up in both tables.
I ran into the same issue just now. In my case, all I had to do is to make sure that the table I am referencing in the foreign key must be created prior to the current table (earlier in the code). So if you are referencing a variable (x*5) the system should know what x is (x must be declared in earlier lines of code). This resolved my issue, hope it'll help someone else.
The problem is very simple to solve
e.g: you have two table with names users and posts and you want create foreign key in posts table and you use phpMyAdmin
1) in post table add new column
(name:use_id | type: like the id in user table | Length:like the id in user table | Default:NULL | Attributes:unsigned | index:INDEX )
2)on Structure tab go to relation view
(Constraint name: auto set by phpmyAdmin | column name:select user_id |table:users | key: id ,...)
It was simply solved
javad mosavi iran/urmia
I had the same error, and I discovered that on my own case, one table was MyISAM, and the other one INNO. Once I switched the MyISAM table to INNO. It solved the issue.
One more solution which I was missing here is, that each primary key of the referenced table should have an entry with a foreign key in the table where the constraint is created.
If U Table Is Myisum And New Table Is InoDb you Are Note Foreign
You Must Change MyIsum Table To InoDb

Bogus foreign key constraint fail

I get this error message:
ERROR 1217 (23000) at line 40: Cannot
delete or update a parent row: a
foreign key constraint fails
... when I try to drop a table:
DROP TABLE IF EXISTS `area`;
... defined like this:
CREATE TABLE `area` (
`area_id` char(3) COLLATE utf8_spanish_ci NOT NULL,
`nombre_area` varchar(30) COLLATE utf8_spanish_ci NOT NULL,
`descripcion_area` varchar(100) COLLATE utf8_spanish_ci NOT NULL,
PRIMARY KEY (`area_id`),
UNIQUE KEY `nombre_area_UNIQUE` (`nombre_area`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
The funny thing is that I already dropped all other tables in the schema that have foreign keys against area. Actually, the database is empty except for the area table.
How can it possibly have child rows if there isn't any other object in the database? As far as I know, InnoDB doesn't allow foreign keys on other schemas, does it?
(I can even run a RENAME TABLE area TO something_else command :-?)
On demand, now as an answer...
When using MySQL Query Browser or phpMyAdmin, it appears that a new connection is opened for each query (bugs.mysql.com/bug.php?id=8280), making it neccessary to write all the drop statements in one query, eg.
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE my_first_table_to_drop;
DROP TABLE my_second_table_to_drop;
SET FOREIGN_KEY_CHECKS=1;
Where the SET FOREIGN_KEY_CHECKS=1 serves as an extra security measure...
Two possibilities:
There is a table within another schema ("database" in mysql terminology) which has a FK reference
The innodb internal data dictionary is out of sync with the mysql one.
You can see which table it was (one of them, anyway) by doing a "SHOW ENGINE INNODB STATUS" after the drop fails.
If it turns out to be the latter case, I'd dump and restore the whole server if you can.
MySQL 5.1 and above will give you the name of the table with the FK in the error message.
Disable foreign key checking
SET FOREIGN_KEY_CHECKS=0
from this blog:
You can temporarily disable foreign key checks:
SET FOREIGN_KEY_CHECKS=0;
Just be sure to restore them once you’re done messing around:
SET FOREIGN_KEY_CHECKS=1;
hopefully its work
SET foreign_key_checks = 0;
DROP TABLE table name;
SET foreign_key_checks = 1;
On Rails, one can do the following using the rails console:
connection = ActiveRecord::Base.connection
connection.execute("SET FOREIGN_KEY_CHECKS=0;")
Maybe you received an error when working with this table before. You can rename the table and try to remove it again.
ALTER TABLE `area` RENAME TO `area2`;
DROP TABLE IF EXISTS `area2`;
i found an easy solution, export the database, edit it what you want to edit in a text editor, then import it. Done
Cannot delete or update a parent row: a foreign key constraint fails (table1.user_role, CONSTRAINT FK143BF46A8dsfsfds##5A6BD60 FOREIGN KEY (user_id) REFERENCES user (id))
What i did in two simple steps . first i delete the child row in child table like
mysql> delete from table2 where role_id = 2 && user_id =20;
Query OK, 1 row affected (0.10 sec)
and second step as deleting the parent
delete from table1 where id = 20;
Query OK, 1 row affected (0.12 sec)
By this i solve the Problem which means Delete Child then Delete parent
i Hope You got it. :)