【发布时间】: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