【问题标题】:StatementCallback; bad SQL grammar only when deployed to Heroku语句回调;仅在部署到 Heroku 时才出现错误的 SQL 语法
【发布时间】:2019-10-06 03:31:09
【问题描述】:

我创建了使用 jdbctemplate 的 spring-boot 应用程序,在 localhost 上一切正常,但是当我将应用程序部署到 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);
}

【问题讨论】:

  • 要么:1) 表“products”在部署的环境中不存在,2) 表名中有拼写错误(驼峰式、蛇式、小写、大写),或者如果这些是好,那么这个:stackoverflow.com/questions/36753568/…
  • 就像我说的,它可以在本地机器上运行

标签: java spring-boot heroku jdbc jdbctemplate


【解决方案1】:

我认为您应该像这样将架构名称放在 application.properties 文件中:

spring.jpa.properties.hibernate.default_schema=public

然后像这样写你的查询:

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

来源:this answerthis answer

【讨论】:

  • 我没有使用 jpa/hibernate,只是 jdbctemplate
【解决方案2】:

【讨论】:

    【解决方案3】:

    好的,我修好了。

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

    I used command from this answer 在 Heroku CLI 中从我的应用程序中删除 heroku 数据库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多