由于PostgreSQL数据库上的“重复”索引,Liquibase无法执行迁移

时间:2017-05-27 21:31:18

标签: java spring spring-boot liquibase

我有一个Liquibase迁移作为我的Spring Boot应用程序的一部分来初始化我的数据库。出于开发目的,在H2内存数据库中,Liquibase已经执行了迁移而没有任何问题。但是,当我针对PostgreSQL数据库运行时,Liquibase无法迁移,因为PostgreSQL返回以下内容:

  

引起:org.postgresql.util.PSQLException:错误:关系“idx_channel_id”已存在

请注意,应用索引的表是全新的,Liquibase迁移文件中没有其他具有相同名称的索引。如果我删除了这个索引创建,它就会在下一个创建时失败。如果删除索引(或将它们移动到通过调用PostgreSQL数据库的前提条件排除的单独迁移),迁移会成功,但根本没有索引。

我一直在摸着头,我被困住了。以下全部Liquibase迁移:

databaseChangeLog:
  - changeSet:
      id: 1
      author: rmorrison
      changes:
        - createTable:
            tableName: shouts
            columns:
              - column:
                  name: id
                  type: bigint
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: discord_id
                  type: varchar(18)
                  constraints:
                    nullable: false
              - column:
                  name: author_id
                  type: varchar(18)
                  constraints:
                    nullable: false
              - column:
                  name: channel_id
                  type: varchar(18)
                  constraints:
                    nullable: false
              - column:
                  name: guild_nickname
                  type: varchar(255)
                  constraints:
                    nullable: false
              - column:
                  name: content
                  type: varchar(2000)
                  constraints:
                    nullable: false
              - column:
                  name: created
                  type: blob
                  constraints:
                    nullable: false
        - createTable:
            tableName: contexts
            columns:
              - column:
                  name: id
                  type: bigint
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: discord_id
                  type: varchar(18)
                  constraints:
                    nullable: false
              - column:
                  name: author_id
                  type: varchar(18)
                  constraints:
                    nullable: false
              - column:
                  name: guild_nickname
                  type: varchar(255)
                  constraints:
                    nullable: false
              - column:
                  name: content
                  type: varchar(2000)
                  constraints:
                    nullable: false
              - column:
                  name: created
                  type: blob
                  constraints:
                    nullable: false
        - createTable:
            tableName: shouts_contexts
            columns:
              - column:
                  name: shout_id
                  type: bigint
                  constraints:
                    nullable: false
              - column:
                  name: context_id
                  type: bigint
                  constraints:
                    nullable: false
        - createIndex:
            indexName: idx_channel_id
            tableName: shouts
            unique: false
            columns:
              - column:
                  name: channel_id
                  type: varchar(18)
        - createIndex:
            indexName: idx_author_channel_id
            tableName: shouts
            unique: false
            columns:
              - column:
                  name: author_id
                  type: varchar(18)
              - column:
                  name: channel_id
                  type: varchar(18)
        - createIndex:
            indexName: idx_content
            tableName: shouts
            unique: true
            columns:
              - column:
                  name: content
                  type: varchar(2000)
编辑:我重新配置PostgreSQL以启用语句记录,现在看到这个 - 看起来这可能不是Liquibase。继续调查...

< 2017-05-27 19:13:19.697 EDT > LOG:  execute <unnamed>: BEGIN
< 2017-05-27 19:13:19.698 EDT > LOG:  execute <unnamed>: CREATE TABLE public.shouts (id BIGSERIAL NOT NULL, discord_id VARCHAR(18) NOT NULL, author_id VARCHAR(18) NOT NULL, channel_id VARCHAR(18) NOT NULL, guild_nickname VARCHAR(255) NOT NULL, content VARCHAR(2000) NOT NULL, created OID NOT NULL, CONSTRAINT PK_SHOUTS PRIMARY KEY (id))
< 2017-05-27 19:13:19.718 EDT > LOG:  execute <unnamed>: CREATE TABLE public.contexts (id BIGSERIAL NOT NULL, discord_id VARCHAR(18) NOT NULL, author_id VARCHAR(18) NOT NULL, guild_nickname VARCHAR(255) NOT NULL, content VARCHAR(2000) NOT NULL, created OID NOT NULL, CONSTRAINT PK_CONTEXTS PRIMARY KEY (id))
< 2017-05-27 19:13:19.725 EDT > LOG:  execute <unnamed>: CREATE TABLE public.shouts_contexts (shout_id BIGINT NOT NULL, context_id BIGINT NOT NULL)
< 2017-05-27 19:13:19.727 EDT > LOG:  execute <unnamed>: CREATE INDEX idx_channel_id ON public.shouts(channel_id)
< 2017-05-27 19:13:19.727 EDT > ERROR:  relation "idx_channel_id" already exists
< 2017-05-27 19:13:19.727 EDT > STATEMENT:  CREATE INDEX idx_channel_id ON public.shouts(channel_id)
< 2017-05-27 19:13:19.741 EDT > LOG:  execute S_1: ROLLBACK

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。事实证明,PostgreSQL要求索引名称在表中是唯一的。我有一个&#34;备份&#34;具有相同索引名称的旧数据表导致冲突。我删除了有问题的表,迁移成功,现在没有问题。