[Errata] Foreign Key Constraints Error

In the Foreign Key Constraints Chapter I am trying to set up a foreign key constraint using the code as outlined in the book:

extension Marker : Migration {
    static func prepare(on connection: PostgreSQLConnection) -> Future<Void> {
        return Database.create(self, on: connection) { builder in
            try addProperties(to: builder)
            try builder.addReference(from: \.userId, to: \User.id)
        }
    }
}

but am getting the following error:

Invalid component of Swift key path

on the addReference line. What is the correct way to do this? (Iā€™m assuming things have changed since that chapter was written?)

Thanks!

I found the fix for the issue:

addReference was deprecated recently, also the full model name needs to be referenced. This worked:

builder.reference(from: \Marker.userId, to: \User.id)
1 Like

@mikebronner Thank you for sharing the solution - much appreciated! :]

1 Like