vapor / fluent-postgres-driver

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

Wrong id is decoded for JOINed table results #112

Closed rafiki270 closed 4 years ago

rafiki270 commented 5 years ago

In case of:

Build.query(on: req).join(\BuildTag.buildId, to: \Build.id)

I get BuildTag.id in my result objects

rafiki270 commented 5 years ago

Tests PR https://github.com/vapor/fluent-postgresql/pull/111

rafiki270 commented 5 years ago

Quick fix is to rename the id column (coding part) of your JOIN model to something like join_id:

/// Join table between Apps and Tags
public final class BuildTag: PostgreSQLUUIDModel {

    public typealias Left = Build
    public typealias Right = Tag

    public static var leftIDKey: WritableKeyPath<BuildTag, DbIdentifier> {
        return \.buildId
    }

    public static var rightIDKey: WritableKeyPath<BuildTag, DbIdentifier> {
        return \.tagId
    }

    public var id: UUID?
    public var buildId: UUID
    public var tagId: UUID

    enum CodingKeys: String, CodingKey {
        case id = "join_id"
        case buildId = "build_id"
        case tagId = "tag_id"
    }

}
tonyarnold commented 4 years ago

I'm seeing the same thing in very similar code.

tanner0101 commented 4 years ago

The issue is described more in-depth here: https://github.com/vapor/fluent-postgres-driver/pull/114

To solve this problem you can run releaseCachedConnections(on:...) causing the schema lookup information to be refreshed.

This is no longer a problem in Fluent 4 since unambiguous column aliases are used, i.e.:

SELECT id AS todos_id FROM todos