Delete table with the foreign key foreign key constraint fails [error]


title: Delete a table with a foreign key foreign key constraint fails [error]
DATE: 2018-08-02 21:59:06
Tags: database
---

Way back when the school was being hibernate, just learn-to-many, many-associated operations. It is also just the time it was with a project, the structure between the tables also sort out, saying we must learn to use, put the structure of these tables can configure it with regard to cascade the on. I did not expect is here to give himself a small pit. A few days ago to see someone in a post that as little as possible with some ORM constraints, what's database foreign key constraint. It was also disagree. I did not expect to encounter this problem, perhaps for database operations still not very familiar with too many people do not really constraint. However, there is naturally has its benefits.

Today, delete data in a table of the time reported the following error:

09:55:49.144 [http-nio-8080-exec-6] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Cannot delete or update a parent row: a foreign key constraint fails (`checkin`.`right_umenu`, CONSTRAINT `FKnmg8itd642tdyn6qh1q42h60r` FOREIGN KEY (`menu_detailPid`) REFERENCES `menu_detail` (`id`))
09:55:49.151 [http-nio-8080-exec-6] ERROR org.hibernate.internal.SessionImpl - HHH000346: Error during managed flush [could not execute statement]


org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3198)
    .......
    

After the Internet to find the results obtained and I think it is the same, the problem is caused by the foreign key (of course, an error message is written in a foreign key do not know what, how that line (● ∀ ●)) . Online is to say: to force the generation of foreign keys between tables in the delete operation, the relationship between the database will not be deleted checklist lead.

Solution :

SET foreign_key_checks = 0;  // 先设置外键约束检查关闭
 
drop table table1;  // 删除表,如果要删除视图,也是如此
 
SET foreign_key_checks = 1; // 开启外键约束检查,以保持表结构完整性

principle:

There is a foreign_key_checks MySQL environment variable, which is the default configuration item check foreign key, if it is set to 0, the foreign key constraints are not checked. Foreign_key_checks see the value of:

show VARIABLES like "foreign%";

However, however . I even put a foreign key constraint checking shut down, but it is also being given in open places to check. That I had no choice.

But when I try to delete the item from the foreign key RESTRICT instead CASCADE not only can delete without affecting my other execution method when, but also more convenient to perform the way I wanted to write.

Went back to check, this option is a database defining foreign keys, you can know that words can later update and delete with the four operation:

no action , set null , set default ,cascade 。

no action 表示 不做任何操作,

set null 表示在外键表中将相应字段设置为null

set default 表示设置为默认值(restrict)

cascade 表示级联操作,就是说,如果主键表中被参考字段更新,外键表中也更新,主键表中的记录被删除,外键表中改行也相应删除

So when I put such a set of cascading deletes also want to perform a lot of things have to go up a database of all ah.

Guess you like

Origin www.cnblogs.com/flytree/p/11622635.html