문제

I want to add a unique constraint to a column using Liquibase. Of course I want to check if duplicate rows exist using a precondition.

I came up this:

<preConditions>
    <sqlCheck expectedResult="0">
        select count(*)
        from person
        having ( count(username) > 1 )
    </sqlCheck>
</preConditions>

However this produces Empty set on MySQL and probably other databases.

I tried using expectedResult="" and expectedResult="null" but both don't work.

도움이 되었습니까?

해결책

You could always force a result:

select 
  case when exists(
    select username, count(*)
    from person
    group by username
    having count(*) > 1 )
    then 1
    else 0
  end

This also allows a more normal group by/having

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top