Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create automatically tables at startup #748

Open
mmaryo opened this issue Mar 24, 2022 · 6 comments
Open

Create automatically tables at startup #748

mmaryo opened this issue Mar 24, 2022 · 6 comments
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@mmaryo
Copy link

mmaryo commented Mar 24, 2022

Hello

It looks like tables are not created
Postgres stay empty

Even with spring.jpa.hibernate.ddl-auto=create

This is my entity

@Table(schema = "common", name = "\"Exchanges\"")
data class Exchanges (
    @Column("ID") @Id var id: Int = 0,
    @Column("KaikoExchangeCode") var kaikoExchangeCode: String? = null,
)

And the repository

interface ExchangesRepository : R2dbcRepository<Exchanges, Long>

I use Spring Boot 3.0.0.M1, WebFlux, Jdk 17, Kotin, R2DBC

Do you have an idea? How to debug and find a solution?

EDIT
Also I try to connect to H2 db
spring.r2dbc.url=r2dbc:h2:mem:///testdb
Connection is done
But test end with

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "COMMON" not found; SQL statement:
SELECT common."Exchanges".* FROM common."Exchanges" [90079-210]

That means the table is not created at test startup

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 24, 2022
@mmaryo
Copy link
Author

mmaryo commented Mar 25, 2022

I did a little project
Please find the attachment

MappingTest.zip

This test

@SpringBootTest
class OneTest {
    @Autowired
    lateinit var exchangesRepository: ExchangesRepository
    @Test
    fun test() {
        exchangesRepository.save(Exchanges(44, "66")).block()
        val res = exchangesRepository.findAll().collectList().block()
        println(res)
    }

}

Throw this error

org.springframework.r2dbc.BadSqlGrammarException: execute; bad SQL grammar [UPDATE common."ExchangesCodesMapping" SET ExchangeCode = $1 WHERE common."ExchangesCodesMapping".ID = $2]; nested exception is io.r2dbc.spi.R2dbcBadGrammarException: [90079] [90079] Schema "COMMON" not found; SQL statement:
UPDATE common."ExchangesCodesMapping" SET ExchangeCode = $1 WHERE common."ExchangesCodesMapping".ID = $2 [90079-210]

Notice when I run the server to an existing database ExchangesRepository works very well
It seems this code is unable to create the table at startup

@mmaryo
Copy link
Author

mmaryo commented Apr 7, 2022

It seems that is related to NamingStrategy
How to override it ?
I need to add a double quote and keep the case of string inside @column("Name")

@mmaryo
Copy link
Author

mmaryo commented Apr 7, 2022

open class R2dbcConfiguration : AbstractR2dbcConfiguration() {
    override fun connectionFactory(): ConnectionFactory {
        throw ExecutionControl.NotImplementedException("No need")
    }

    override fun r2dbcMappingContext(namingStrategy: Optional<NamingStrategy>, r2dbcCustomConversions: R2dbcCustomConversions): R2dbcMappingContext {
        val context = super.r2dbcMappingContext(namingStrategy, r2dbcCustomConversions)
        context.isForceQuote = true
        return context
    }
}

@linghengqian
Copy link

spring.jpa.hibernate.ddl-auto is completely unrelated to R2DBC

@Kabir1506
Copy link

@mmaryo did you find any solution to this?

@franciskinyuru
Copy link

Below worked for me in a configuration class and below bean
@Bean public ConnectionFactoryInitializer connectionFactoryInitializer(ConnectionFactory connectionFactory) { ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer(); initializer.setConnectionFactory(connectionFactory); ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.addScript(new ClassPathResource("schema.sql")); initializer.setDatabasePopulator(populator); return initializer; }

Then have you schema.sql in the resources folder

Finally add below in your application.properties or yml file
spring.resources.static-locations=classpath:/,classpath:/resources/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

5 participants