Bug:CreateTableIfNotexsits when meet the unique key.Error Happend

23 views
Skip to first unread message

Copper Friddle

unread,
Jan 12, 2015, 3:48:22 AM1/12/15
to ormli...@googlegroups.com
I use the creatTableIfNotExists.when I use  uniqueIndexName to make a combon unqiue.but i meet this prolems:
Error Info:

java.sql.SQLException: SQL statement failed: CREATE UNIQUE INDEX `publisher_uniqueKey` ON `publisher` ( `userId`, `commercialType` )
at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
at com.j256.ormlite.table.TableUtils.doStatements(TableUtils.java:468)
at com.j256.ormlite.table.TableUtils.doCreateTable(TableUtils.java:442)
at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:220)
at com.j256.ormlite.table.TableUtils.createTableIfNotExists(TableUtils.java:61)
at com.trendata.crawler.house.BaseHouse.Database.HouseOrmLite.createTables(HouseOrmLite.java:122)
at com.trendata.crawler.house.BaseHouse.Database.HouseOrmLite.init(HouseOrmLite.java:88)
at com.trendata.crawler.house.BaseHouse.Database.HouseOrmLite.<init>(HouseOrmLite.java:107)
at com.trendata.crawler.house.BaseHouse.Default.SavedCrawlStrategy.<init>(SavedCrawlStrategy.java:41)
at com.trendata.crawler.house.Houses.FiveEightCrawler.House58CrawlerControllerTest.fuckExecute(House58CrawlerControllerTest.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Duplicate key name 'publisher_uniqueKey'


I debug it:find that.
In the Class:TableUtils.class,in method:doCreateTable

private static <T, ID> int doCreateTable(ConnectionSource connectionSource, TableInfo<T, ID> tableInfo,
boolean ifNotExists) throws SQLException {
DatabaseType databaseType = connectionSource.getDatabaseType();
logger.info("creating table '{}'", tableInfo.getTableName());
List<String> statements = new ArrayList<String>();
List<String> queriesAfter = new ArrayList<String>();
addCreateTableStatements(databaseType, tableInfo, statements, queriesAfter, ifNotExists);
DatabaseConnection connection = connectionSource.getReadWriteConnection();
try {
int stmtC =
doStatements(connection, "create", statements, false, databaseType.isCreateTableReturnsNegative(),
databaseType.isCreateTableReturnsZero());
stmtC += doCreateTestQueries(connection, databaseType, queriesAfter);
return stmtC;
} finally {
connectionSource.releaseConnection(connection);
}
}


doStatements:has two String:
statements[0]:CREATE TABLE IF NOT EXISTS `tablename` (...........) ENGINE=InnoDB 
statements[1]:CREATE UNIQUE INDEX `uniquekye` ON `tablename` ( `field1`, `field2` )


when i use createTableIfNotExists:
it means that the create table will execute two sentences,one sentences means that the right things:if find table drop it.
but the second sentences meet the problem:create unique index--->it already has the uniqueky.so report crash.

Hope someone fix the bug!.
PS:forgive my poor english
Reply all
Reply to author
Forward
0 new messages