Kitura / Swift-Kuery-ORM

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

UUID type ID #120

Closed zhenyab closed 2 years ago

zhenyab commented 5 years ago

Context and Description

I use existing database and try to create ORM object to use with it. But I found it is impossible, or may be can't find workaround.

Environment Details

macOS: 10.14.6 Xcode: 10.3 Swift: Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5) Target: x86_64-apple-darwin18.7.0

Steps to Reproduce

  1. I create struct with the UUID type for ID of the table
struct Device : Model {
    var id: UUID
    var command: String
    var recipe: String
    var isEnabled: Bool

    static var tableName = "devices"
    static var idColumnName = "id"
    static var idColumnType = UUID
}

2) Later I try to use the object to find entry by id like in following code:

let uuid = UUID(uuidString: deviceId)

            if uuid != nil {

                Device.find(id: uuid) { result, error in
                    guard let result = result else {
                        Log.warning("No such device with id: \(deviceId)")
                        return
                    }
                    print(result.command)
                }
            } else {
                Log.warning("UUID is not valid: \(deviceId)")
            }

Expected vs. Actual Behaviour

nathanfallet commented 3 years ago

Try with this snippet: (if UUID conforms to Identifier then it will work)

if let uuid = UUID(uuidString: deviceId) {
    Device.find(id: uuid) { result, error in
        guard let result = result else {
            Log.warning("No such device with id: \(deviceId)")
            return
        }
        print(result.command)
    }
} else {
    Log.warning("UUID is not valid: \(deviceId)")
}

Your problem might occurs because you didn't unwrap uuid, what I've fixed here.

zhenyab commented 3 years ago

Many thanks for the hint

nathanfallet commented 3 years ago

@zhenyab If it works as expected, you can close the issue.