vapor / fluent-postgres-driver

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

Issue with model identifiers when they aren't named "id" #123

Closed PopFlamingo closed 4 years ago

PopFlamingo commented 5 years ago

I'm not sure to understand exactly how id and fluentID are used by Fluent, but from what I can tell, in:

private struct PostgresReturning: SQLExpression {
    let base: SQLExpression
    init(_ base: SQLExpression) {
        self.base = base
    }

    func serialize(to serializer: inout SQLSerializer) {
        self.base.serialize(to: &serializer)
        serializer.write(#" RETURNING id as "fluentID""#)
    }
}

It doesn't seem ok to have id hardcoded in RETURNING id as "fluentID", instead it should be the name of the primary key of the model, whatever it is.

What do you think?

PopFlamingo commented 5 years ago

The way I noticed this is that I got errors since my model identifiers aren't named id.

Hejki commented 4 years ago

Hi, I have same problem. It can be fixed if someone agree with this changes in fluent-kit.

Then PostgresReturning struct can be changed to

private struct PostgresReturning: SQLExpression {
    let base: SQLExpression
    let idKey: String

    init(_ base: SQLExpression, _ idKey: String) {
        self.base = base
        self.idKey = idKey
    }

    func serialize(to serializer: inout SQLSerializer) {
        self.base.serialize(to: &serializer)
        serializer.write(#" RETURNING "\#(idKey)" as "fluentID""#)
    }
}

// and change call initializer in _FluentPostgresDatabase extension
expression = PostgresReturning(expression, query.idKey)
tanner0101 commented 4 years ago

Fixed. Thank you for reporting.