vapor / fluent-postgres-driver

🐘 PostgreSQL driver for Fluent.
MIT License
149 stars 53 forks source link

Adding unique on update fails #54

Closed joscdk closed 6 years ago

joscdk commented 6 years ago

When modifying a table after creation, the migration fails, when adding index and unique to an existing field.

This code fails

static func prepare(on connection: PostgreSQLConnection) -> Future<Void> {
        return Database.update(User.self, on: connection) { builder in
            try builder.addIndex(to: \.username, isUnique: true)
        }
    }

With the following error

screen shot 2018-05-15 at 9 40 47 pm
tanner0101 commented 6 years ago
struct AddUserIndex: PostgreSQLMigration {
    static func prepare(on conn: PostgreSQLConnection) -> Future<Void> {
        return Database.update(User.self, on: conn) { builder in
            builder.unique(on: \.username)
        }
    }

    static func revert(on conn: PostgreSQLConnection) -> Future<Void> {
        return Database.update(User.self, on: conn) { builder in
            builder.deleteUnique(from: \.username)
        }
    }
}

Results in

[psql] [2018-07-26 01:11:31 +0000] ALTER TABLE "User #1" ADD CONSTRAINT "uq:User #1.username" UNIQUE ("username") []
[psql] [2018-07-26 01:11:31 +0000] ALTER TABLE "User #1" DROP CONSTRAINT "uq:User #1.username" []

Looks to be fixed. Thanks!