Kitura / Swift-Kuery-ORM

An ORM for Swift, built on Codable
Apache License 2.0
212 stars 30 forks source link

enum issue when adopting Model #89

Closed diuming closed 5 years ago

diuming commented 5 years ago

enum issue when adopting Model

Swift-Kuery-ORM: 0.4.1 PostgreSQL: 10.6

other questions: R1) 701 : Nested structs or dictionaries are not supported R2) 701 : Arrays or sets are not supported How do I get DB connection or pool from Database.default? Do you have any plan to support R1 and R2?

create an object and save it

ColorComponent(type: .C, value: 45.0).save { (component, error) in
    print(String(describing: error))
}

Optional(706 : Query execution error:
ERROR:  invalid input syntax for integer: "C"
 For query: INSERT INTO "ColorComponents" ("value", "type") VALUES ($1, $2))

swift code

enum CMYKChannel: Int, Codable {
    case C = 0
    case M = 1
    case Y = 2
    case K = 3
}
final class ColorComponent: Model {
    static var tableName =  "ColorComponents"
    var value: Double
    var type: CMYKChannel
    init(type: CMYKChannel, value: Double) {
        self.value = value
        self.type = type
    }
}
kilnerm commented 5 years ago

@diuming, With regard to your questions R1 and R2 we are planning on adding support for nested structs, arrays and dictionaries along with relations. Currently however the focus is on resolving functional issues to build a stable base to build these enhancements on top of.

Database does not expose an API for getting a connection, at this time you would have to keep a reference to the connection pool you initialised it with and use that directly.

I will do some investigation into the enum issue and update once I have some findings.

diuming commented 5 years ago

@kilnerm I got it and thank you for helping me.

kilnerm commented 5 years ago

@diuming, Did you resolve your issue with the enum and Model?

In looking at the issue yesterday I understand why the problem occurs but could not see a way to resolve it. When the ColorComponent table is created the TypeDecoder identifies the CMYKChannel as an integer field. When the save occurs the DatabaseEncoder is not able to make that determination and the value is encoded as C. I did experiment with using CustomStringConvertible and was able to get the raw value written to the database but then when reading the record back into a Model it fails during decoding as the DatabaseDecoder is unable to support the custom CYMKChannel type.