10

I am using the create-drop option for development, when deploying to mysql database (using hibernate 4) I get the following output:

15:18:07,715 ERROR SchemaExport:426 - HHH000389: Unsuccessful: alter table my_table drop foreign key FKD42DEFE312AC02F1
15:18:07,715 ERROR SchemaExport:427 - Table 'my_db.my_table' doesn't exist

It seems its attempting to alter a table before it has created it. The table and FK are succesfully created. What causes the error message ?

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311

3 Answers3

4

The problem is that:

  • if you have two tables A and B and a foreign key reference in A that references B.
  • and you want to delete the tables

You need to remove the foreign key constraint first. (except you know the right sequence and have no circular dependencies)

So you normaly remove all constrains first and then drop the tables.

If one table does not exists (because it is new) then you use drop table if exists But unfortunaly my-sql as no statement to remove a forainkey only if the table exists. So it is more a lack of features than a bug.


I solved it, but my setup is different. I use hibernate only in validation mode and run the scripts to update the database by hand. The scripts are generated by hibernate org.hibernate.tool.hbm2ddl.SchemaExport. Then I take the generated file, added set foreign_key_checks = 0; in the beginning and set foreign_key_checks = 1; at the end. Then I commented out (add -- at the beginning) every line that match the pattern alter table *. drop foreign key *.;

@See https://stackoverflow.com/a/34038447/280244 for more details

Community
  • 1
  • 1
Ralph
  • 118,862
  • 56
  • 287
  • 383
0

When I was doing this, I was using postgres as my database, and this is an easy fix. What is going on is that these table are being recreated every time, and everything that was in those table before is getting wiped. To fix it, go into your persistence.xml under src/main/resources/META-INF there inside the persistence.xml comment out the line that

<property name="hibernate.hbm2ddl.auto" value="create"/>

That line recreates all the tables with the same foreign key every time and that will drop and recreate all your tables(all your data inside the database will be lost) every time you restart your tomcat/jetty server. If you comment it out, it will stop it from recreating the database and the data in there will stay there.

http://static.springsource.org/spring-roo/reference/html/base-persistence.html

0

Check also the answer in this similar question: Grails 2.4 and hibernate4 errors with run-app

For me it seemed to the case:

It's simply logging an error because the table it's trying to drop doesn't yet exist.

Community
  • 1
  • 1
George
  • 769
  • 4
  • 11
  • 31