Reason: liquibase.exception.DatabaseException: ERROR: column "id" is of type bigint but expression is of type text
Reason: liquibase.exception.DatabaseException: ERROR: column "id" is of type bigint but expression is of type text
我想创建一个 SQL 脚本,它使用 Liquibase 生成测试数据。我试过这个:
实体:
@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 自动生成数据库 table id,而我只插入新数据。
你知道我该如何解决这个问题吗?
您需要在 INSERT 语句中指定目标列。事实上,这样做是很好的编码习惯,应该 总是 这样做,而不仅仅是在出现错误时才这样做。目标 table 列和 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
子句中。
我想创建一个 SQL 脚本,它使用 Liquibase 生成测试数据。我试过这个:
实体:
@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 自动生成数据库 table id,而我只插入新数据。 你知道我该如何解决这个问题吗?
您需要在 INSERT 语句中指定目标列。事实上,这样做是很好的编码习惯,应该 总是 这样做,而不仅仅是在出现错误时才这样做。目标 table 列和 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
子句中。