Kitura / Swift-Kuery-ORM

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

Does ORM support subclass? #127

Closed diuming closed 4 years ago

diuming commented 4 years ago

I try to use my subclass ClassB conforms with Model. Then create database table using createTableSync() function. but only id and value3's are created.

how about value1 and value2 of ClassA?

class ClassA: Codable {
    var value1: Int
    var value2: Int
}

final class ClassB: ClassA {
    id: Int
    var value3: Int
}

extension ClassB: Model {
    static let tableName =  "ClassBs"
}
igorL-Pg commented 4 years ago

Class inheritance works just fine, but you have to rewrite your required init(from decoder: Decoder). This is a bit odd because the Model protocol conforms to Codable protocol and subclass inherits from a class that also conforms to the same protocol. Therefore encoding and decoding for basic data types should be working out fo a box.

igorL-Pg commented 4 years ago

You also have to override func encode(to encoder: Encoder) throws in the case that you want to use save or update function.

diuming commented 4 years ago

@igorL-Pg got it.