H2 Schema initailization. Syntax error in SQL statement

最后都变了- 提交于 2020-07-29 12:23:27

问题


I have a spring boot application and I trying to initialize some data on application startup.

This is my application properties:

#Database connection
spring.datasource.url=jdbc:h2:mem:test_db
spring.datasource.username=...
spring.datasource.password=...
spring.datasource.driverClassName=org.h2.Driver

spring.datasource.initialize=true
spring.datasource.schema=schema.sql
spring.datasource.data=schema.sql


#Hibernate configuration
#spring.jpa.hibernate.ddl-auto = none

This is schema.sql:

CREATE TABLE IF NOT EXISTS `Person` (
  `id`         INTEGER  PRIMARY KEY AUTO_INCREMENT,
  `first_name` VARCHAR(50) NOT NULL,
  `age`        INTEGER  NOT NULL,
  PRIMARY KEY(`id`)
);

and data.sql

INSERT INTO `Person` (
  `id`,
  `first_name`,
  `age`
) VALUES (
  1,
  'John',
  20
);

But I got 'Syntax error in SQL statement' on application startup:

19:08:45.642 6474 [main] INFO  o.h.tool.hbm2ddl.SchemaExport - HHH000476: Executing import script '/import.sql'
19:08:45.643 6475 [main] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000388: Unsuccessful: CREATE TABLE Person (
19:08:45.643 6475 [main] ERROR o.h.tool.hbm2ddl.SchemaExport - Syntax error in SQL statement "CREATE TABLE PERSON ( [*]"; expected "identifier"
Syntax error in SQL statement "CREATE TABLE PERSON ( [*]"; expected "identifier"; SQL statement:

I can't understand, what's wrong with this SQL.


回答1:


Try this code. Remove PRIMARY KEY(id) and execute it.

CREATE TABLE IF NOT EXISTS `Person` (
    `id`         INTEGER  PRIMARY KEY AUTO_INCREMENT,
     `first_name` VARCHAR(50) NOT NULL,
     `age`        INTEGER  NOT NULL
);


来源:https://stackoverflow.com/questions/44267245/h2-schema-initailization-syntax-error-in-sql-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!