Closed FredericRuaudel closed 4 years ago
This was happening because Postgres' table name cache is outdated and it is unable to resolve the difference between the two columns. After running migrations you should call:
try! app.requestCachedConnection(to: .psql).wait()
This will reload postgres' table name cache allowing both status
fields to be distinguishable.
FluentPostgreSQL version 1.0.0 PostgreSQL version 1.0.1 Vapor Core version: 3.4.2 (see
Package.resolved
in joined example project for other module versions)I've recently stumble upon a bug while joining two tables that both have a
PostgreSQLEnum
field with the same name. And it causes a weird bug that was either:NIO-ELT-#1 (5): EXC_BAD_ACCESS
errormalloc: *** error for object 0x10383c710: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug
failed: caught error: The data couldn’t be read because it isn’t in the correct format.
DecodingError.dataCorrupted: Cannot initialize AttachmentStatus from invalid String value done
(in the case of the attached sample project)usually, it crashes half of the times and succeeds the other half with a perfect 1 for 1 rate
So to show the bug, I've created a simple sample project based on Todo template (see attached zip) with this simple DB schema to show the problem. You need to create a PG database named
joinenumbug
to make it runs and runs the tests of the app to get the bug.We have a
Todo
table with astatus
field of typeEnum TodoStatus
and aAttachment
table with also astatus
field but of another typeEnum AttachmentStatus
and they are linked via theTodo
'sattachmentId
foreignKey.So when I perform a join between this two tables, the result has two columns with the same name but with different data types inside:
and it confuses the decoder which is trying to pass all the status values to the
RawRepresentable
decodableinit
method and as the value doesn't matches any value, it throws an error in the best scenarioYou can also see that clearly if you redefined the decodable
init
for the enum like this:it will display:
showing the problem.
The only workaround I've found is to rename one of the two fields with a different name but it's not a really satisfying solution 😉
Hope you can help!
Thanks in advance!
JoinEnumBug.zip