Grails dbm-update失败:MySQLSyntaxErrorException:您的SQL语法中有错误

时间:2015-07-18 11:05:18

标签: java hibernate grails database-migration spatial

我使用Grails数据库迁移。在我最新的数据库更新中,我创建了下表:

<div>Canvas for put ImageData:</div>
<canvas id="bigCanvas" width="300" height="200"></canvas>
<hr/>
<div>Canvas for get ImageData:</div>
<canvas id="testCanvas" width="300" height="200"></canvas>

我使用import java.security.MessageDigest class City { String id Date dateCreated Date lastUpdated String name static constraints = { id maxSize: 16, unique: true name nullable: false, blank: false } static mapping = { id generator:'assigned' } def setIdIfMissing() { if (!id) { String uuid = UUID.randomUUID().toString() MessageDigest sha1 = MessageDigest.getInstance("SHA1") byte[] digest = sha1.digest(uuid.getBytes()) def tmpId = new BigInteger(1, digest).toString(16) id = tmpId[0..15] // size of the id } } /** * Constructor */ City() { setIdIfMissing() } } 创建了变更集。这是

grails dbm-gorm-diff Update4.groovy -add

这是数据库配置。我也使用hibernate空间方言:

databaseChangeLog = {

    changeSet(author: "mg (generated)", id: "1437215773652-1") {
        createTable(tableName: "city") {
            column(name: "id", type: "varchar(16)") {
                constraints(nullable: "false", primaryKey: "true", primaryKeyName: "cityPK")
            }

            column(name: "version", type: "bigint") {
                constraints(nullable: "false")
            }

            column(name: "date_created", type: "datetime") {
                constraints(nullable: "false")
            }

            column(name: "last_updated", type: "datetime") {
                constraints(nullable: "false")
            }

            column(name: "name", type: "varchar(255)") {
                constraints(nullable: "false")
            }
        }
    }

    changeSet(author: "mg (generated)", id: "1437215773652-2") {
        createIndex(indexName: "id_uniq_1437215773559", tableName: "city", unique: "true") {
            column(name: "id")
        }
    }
}

然后我运行``grails dbm-update```导致以下错误:

dataSource {
  pooled = true
  jmxExport = true
  driverClassName = "com.mysql.jdbc.Driver"
  username = "sa"
  password = ""
}
hibernate {
  cache.use_second_level_cache = true
  cache.use_query_cache = false
  cache.region.factory_class = 'org.hibernate.cache.SingletonEhCacheRegionFactory' // Hibernate 3

  singleSession = true // configure OSIV singleSession mode
  flush.mode = 'manual' // OSIV session flush mode outside of transactional context
}

// environment specific settings
environments {
  development {
    dataSource {
      url = "jdbc:mysql://localhost/test?useUnicode=yes&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"
      username = "root"
      password = ""
      dialect = org.hibernatespatial.mysql.MySQLSpatialInnoDBDialect
      pooled = true
      properties {
        maxActive = -1
        minEvictableIdleTimeMillis=1800000
        timeBetweenEvictionRunsMillis=1800000
        numTestsPerEvictionRun=3
        testOnBorrow=true
        testWhileIdle=true
        testOnReturn=true
        validationQuery = "select 1"
      }
    }
  }
  test {
    dataSource {
      dbCreate = "update"
      url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
    }
  }

}

导致此错误的原因是什么?如何预防?

1 个答案:

答案 0 :(得分:0)

我不得不将此作为答案,使输出更清晰:

在错误消息中首次尝试使用SQL命令:

    mysql> CREATE TABLE city (id VARCHAR(16) NOT NULL, version BIGINT NOT NULL, date_created DATETIME NOT NULL, last_updated DATETIME NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT cityPK PRIMARY KEY (id)) type=InnoDB;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 1

现在让我们将type = InnoDB更改为engine = InnoDB并再次运行:

mysql> CREATE TABLE city (id VARCHAR(16) NOT NULL, version BIGINT NOT NULL, date_created DATETIME NOT NULL, last_updated DATETIME NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT cityPK PRIMARY KEY (id)) engine=InnoDB;
Query OK, 0 rows affected (0.46 sec)

Presto有效。

由于某种原因,SQL生成的create语句应该是engine = InnoDB而不是type = InnoDB。