Currently there are no answers on this question on the stackoverflow.
https://stackoverflow.com/questions/43015852/springboot-doesnt-create-the-database-schema?rq=1
I initialized Spring Boot app using https://start.spring.io/
Selected dependencies: JPA, Rest Repositories, MySQL
I set up data source for MySQL schema with name 'test' that doesn't exists and put the following properties inside application.properties:
_spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.generate-unique-name=true
spring.datasource.username = *
spring.datasource.password = *
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.initialization-mode=always
spring.datasource.type=com.mysql.jdbc.jdbc2.optional.MysqlDataSource_
_spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = create_
I expected that Hibernate would create schema with name 'test' for me but this hasn't happend and Spring Boot throws com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException ("Unknown database 'test2'")
It seems like "spring.jpa.hibernate.ddl-auto=create" creates schema only for embedded DBs (like HSQLDB or H2). I didn't find information on this in Spring Boot reference guide. Could you please tell me whether it is an issue or works as expected?
The Spring Boot reference guide has the following note about spring.jpa.hibernate.ddl-auto in this section:
_In a JPA-based app, you can choose to let Hibernate create the schema or use schema.sql_ ...
By the way, the following official guide explains possible values for spring.jpa.hibernate.ddl-auto property as follows:
create Creates the database every time, but don鈥檛 drop it when close.
create-drop Creates the database then drops it when the SessionFactory closes.
But seems like creating of the database not happens at all.
That question has been answered by the OP (it was corrupted dependency or a setup glitch).
It seems you're generally conflating database and schema. You do have to create the database yourself and configure the appropriate users/rights on it. The options you're mentioning are about applying the schema to that existing database.
In memory databases do this automatically, but you can also activate that option in the connection URL (available depending on the chosen database technology). In general, it is a good practice to decouple database creation from schema maintenance.
For other questions, please use StackOverflow.
I have same issue after spring boot 1.5 to 2.0 migration. my tables now aren't generated automatically
@deviant-studio Please read the relevant section of the migration guide and follow up on Stack Overflow with any further questions.
Hi,
I got the same issue with spring.jpa.hibernate.ddl-auto=create in my Spring Boot application.
However, I could achieve this with MySQL flag 'createDatabaseIfNotExist=true' in the url.
spring.datasource.url=jdbc:mysql://localhost:3309/course_api_db?createDatabaseIfNotExist=true
is working fine for me.
Thanks,
Sanjay
Most helpful comment
Hi,
I got the same issue with spring.jpa.hibernate.ddl-auto=create in my Spring Boot application.
However, I could achieve this with MySQL flag 'createDatabaseIfNotExist=true' in the url.
spring.datasource.url=jdbc:mysql://localhost:3309/course_api_db?createDatabaseIfNotExist=true
is working fine for me.
Thanks,
Sanjay