vapor / fluent-kit

Swift ORM (queries, models, and relations) for NoSQL and SQL databases
MIT License
211 stars 116 forks source link

1.42.2 no longer supports multiple properties with the same field name #571

Closed Frizlab closed 1 year ago

Frizlab commented 1 year ago

Describe the bug

Hi! I have a model which has multiple properties with the same field ID. This is due to one of the field being the id of the model, and the other being an @Parent relationship to another table. I want both my tables to have the same ID.

Before FluentKit 1.42.2 version, it worked great, now I get the error column "user_id" specified more than once. Specifically, the generated SQL request is

INSERT INTO "consumer_profiles" ("user_id", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "user_id" [[F7C050A3-BA5A-4720-8536-3CEC7A7B7484, F7C050A3-BA5A-4720-8536-3CEC7A7B7484, 2023-06-22 11:59:05 +0000, 2023-06-22 11:59:05 +0000]] -- database-id: "psql" - psql_connection_id: "1"

As we can see, the user_id column is there twice.

To Reproduce

  1. Creat a Vapor project;
  2. Create a model like so:

    final class ConsumerProfile : Model {
    static let schema = "consumer_profiles"
    
    @ID(custom: "user_id", generatedBy: .user)
    var id: UUID?
    @Parent(key: "user_id")
    var user: User
    }
    final class User : Model {
    static let schema = "users"
    @ID
    var id: UUID?
    }
  3. Create a User
  4. Create a ConsumerProfile

Example:

let user = User()
try await user.save(on: db)
let consumer = ConsumerProfile()
consumer.id = try user.requireID()
try await consumer.save(on: db)

Expected behavior

FluentKit should detect the same column ID is used twice and remove one of them if the values are equal, or throw if they are not.

Environment

Additional context

I have explicitly tested, with FluentKit 1.42.1 I have the expected behavior, with FluentKit 1.42.2 I do not, all other versions of my dependencies being equal.

0xTim commented 1 year ago

Dupe of https://github.com/vapor/fluent-kit/issues/564