vapor / fluent-postgres-driver

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

Postgres error thrown when using advised UUID ID field #170

Closed jbehrens94 closed 3 years ago

jbehrens94 commented 3 years ago

Hi all, thank you very much for your work on the Postgres driver for Vapor! I'm running into issues with a Postgres 13.0 (latest from homebrew) database, and Vapor, while using UUID id's.

I declared the ID field as such:

@ID(key: .id)
var id: UUID?

With a migration that uses:

.field("id", .uuid, .identifier(auto: true))

The Postgres driver is setup like this:

app.databases.use(.postgres(
    hostname: Environment.get("DATABASE_HOST") ?? "localhost",
    username: Environment.get("DATABASE_USERNAME") ?? "postgres",
    password: Environment.get("DATABASE_PASSWORD") ?? "",
    database: Environment.get("DATABASE_NAME") ?? "mobile_backend"
), as: .psql)

app.migrations.add(UserMigration())
app.migrations.add(TokenMigration())
app.migrations.add(RuleMigration())

try app.autoMigrate().wait()

Which results in the error: [ ERROR ] identity column type must be smallint, integer, or bigint (init_params) Fatal error: Error raised at top level: previousError(server: identity column type must be smallint, integer, or bigint (init_params)): file /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.8.25.8/swift/stdlib/public/core/ErrorType.swift, line 200

siemensikkema commented 3 years ago

@jbehrens94 This happens because you are trying to use and auto-incrementing identifier of UUID type (.identifier(auto: true)). If you set auto: false it should work.

By the way, you could simplify the property like so:

@ID
var id: UUID?

and the migration:

.id()