【问题标题】:Reason: liquibase.exception.DatabaseException: ERROR: column "id" is of type bigint but expression is of type text原因:liquibase.exception.DatabaseException:错误:列“id”是 bigint 类型,但表达式是 text 类型
【发布时间】:2021-09-09 08:21:40
【问题描述】:

我想创建一个使用 Liquibase 生成测试数据的 SQL 脚本。我试过这个:

实体:

@Entity
@Table(name = "tasks")
public class Tasks implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, updatable = false, nullable = false)
    private Long id;

    @Column(name = "title", length = 100)
    private String title;

    @Column(name = "meta_title", length = 100)
    private String metaTitle;

    @Column(name = "business_name", length = 100)
    private String businessName;
  ......
}

SQL 脚本:

INSERT into tasks SELECT
    ('Business name ' || generate_series(1,355)) AS business_name,
    (select NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS created_at,
    left (md5(random()::text), 10) AS meta_title,
    left (md5(random()::text), 10) AS status,
    left (md5(random()::text), 10) AS title,
    left (md5(random()::text), 10) AS task_type,
    (select NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS updated_at;

错误:

Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/changes/ch_0001/data/data.yaml::1::test:
     Reason: liquibase.exception.DatabaseException: ERROR: column "id" is of type bigint but expression is of type text
  Hint: You will need to rewrite or cast the expression.
  Position: 42 [Failed SQL: (0) INSERT into tasks SELECT
    ('Business name ' || generate_series(1,355)) AS business_name,
    (select NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS created_at,
    left (md5(random()::text), 10) AS meta_title,
    left (md5(random()::text), 10) AS status,
    left (md5(random()::text), 10) AS title,
    left (md5(random()::text), 10) AS task_type,
    (select NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS updated_at]
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:659)
    at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:53)
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:97)
    at liquibase.Liquibase.update(Liquibase.java:201)
    at liquibase.Liquibase.update(Liquibase.java:178)
    at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:368)
    at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:316)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782)
    ... 19 common frames omitted
Caused by: liquibase.exception.DatabaseException: ERROR: column "id" is of type bigint but expression is of type text
  Hint: You will need to rewrite or cast the expression.
  Position: 42 [Failed SQL: (0) INSERT into tasks SELECT
    ('Business name ' || generate_series(1,355)) AS business_name,
    (select NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS created_at,
    left (md5(random()::text), 10) AS meta_title,
    left (md5(random()::text), 10) AS status,
    left (md5(random()::text), 10) AS title,
    left (md5(random()::text), 10) AS task_type,
    (select NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS updated_at]
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:430)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:87)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:159)
    at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1276)
    at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1258)
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:622)
    ... 27 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: column "id" is of type bigint but expression is of type text
  Hint: You will need to rewrite or cast the expression.
  Position: 42

我希望 Postgress 自动生成数据库表 ID,我只插入新数据。 你知道我该如何解决这个问题吗?

【问题讨论】:

    标签: sql postgresql sql-insert


    【解决方案1】:

    您需要在 INSERT 语句中指定目标列。事实上,这样做是一种很好的编码习惯,应该始终这样做,而不仅仅是当你遇到错误时。目标表列和 SELECT 列之间的匹配是按位置而不是按名称完成的。

    INSERT into tasks (business_name, created_at, meta_title, status, title, ask_type, updated_at)
    SELECT
        'Business name ' || id AS business_name,
        (NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS created_at,
        left (md5(random()::text), 10) AS meta_title,
        left (md5(random()::text), 10) AS status,
        left (md5(random()::text), 10) AS title,
        left (md5(random()::text), 10) AS task_type,
        (NOW() + (random() * (NOW() + '90 days' - NOW())) + '30 days') AS updated_at
    FROM generate_series(1,355) as g(id)
    

    附注:

    在使用now() 函数时不需要使用SELECT(对于md5()random() 都没有这样做)。一般情况下,最好将集合返回函数放入FROM 子句中。

    【讨论】:

    • 感谢您的建议!
    猜你喜欢
    • 2022-08-22
    • 2017-03-28
    • 1970-01-01
    • 2018-10-09
    • 2017-07-22
    • 2015-04-22
    • 2011-12-18
    • 1970-01-01
    • 2015-01-01
    相关资源
    最近更新 更多