语句回调;错误 SQL 语法仅在部署到 Heroku 时

StatementCallback; bad SQL grammar only when deployed to Heroku

我创建了 spring-boot 应用程序,它使用 jdbctemplate,在本地主机上一切正常,但是当我将我的应用程序部署到 Heroku 并访问端点时,我得到了以下信息:

StatementCallback; bad SQL grammar [SELECT id, name, price, image, description FROM products;]; nested exception is org.postgresql.util.PSQLException: ERROR: relation "products" does not exist Position: 49

如果 SQL 查询错误,那么同一个应用程序怎么可能在本地机器上运行?

这是我正在使用的 jdbcTemplate 方法:

public Products getProductsList() {
    ArrayList<Product> productsList = new ArrayList<>();

    jdbcTemplate.query(
            "SELECT id, name, price, image, description FROM public.products;",
            (rs, rowNum) -> new Product(rs.getInt("id"), rs.getString("name"), rs.getFloat("price"), rs.getString("image"), rs.getString("description"))
    ).forEach(product -> productsList.add(product));

    return new Products(productsList);
}

我认为您应该将您的架构名称放在 application.properties 文件中,如下所示:

spring.jpa.properties.hibernate.default_schema=public

然后像这样写你的查询:

SELECT id, name, price, image, description FROM products;

来源:this answer and this answer

您需要使用 https://devcenter.heroku.com/articles/running-database-migrations-for-java-apps

中所述的迁移框架创建表

好的,我修好了

Heroku 出于某种原因将它自己的配置添加到我的项目中(带有 postgresql 的插件)并且它使用来自设置->配置变量 -> DATABASE_URL 的数据库而不是来自 application.properties 的数据库。

在 Heroku CLI 中从我的应用程序中删除 heroku 数据库。