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?
Comment From: emaysyuk
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 ...
Comment From: emaysyuk
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’t 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.
Comment From: bclozel
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.
Comment From: mykola-dev
I have same issue after spring boot 1.5 to 2.0 migration. my tables now aren't generated automatically
Comment From: wilkinsona
@deviant-studio Please read the relevant section of the migration guide and follow up on Stack Overflow with any further questions.
Comment From: sanjay-amoment4u
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
Comment From: amit4aws
I understand its an old post but an alternate solution for specifying the createDatabaseIfNotExist=true to connection url is to type the sql command in the schema file it self. for example :-
CREATE DATABASE IF NOT EXISTS user;
USE user;
CREATE TABLE IF NOT EXISTS usertable (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(80),
country VARCHAR(45) NULL
) engine=InnoDB;
Comment From: Far-Art
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
That worked for me either. Thanks for the help