stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.57k stars 1.54k forks source link

insertMany() fails when encodables have nil on different fields between them. #1130

Closed duhnnie closed 2 years ago

duhnnie commented 2 years ago

insertMany(_ encodables: [Encodable]) fails when optional fields (nullable) have nil as its value on different fields among them.

Getting this message/error:

2022-04-28 09:23:36.842911-0400 SQliteBug[28203:735825] [logging] all VALUES must have the same number of terms in "INSERT INTO "person" ("name", "email") VALUES (?, ?), (?)"
Error The operation couldn’t be completed. (SQLite.Result error 0.)

Build Information

General guidelines

struct Person: Codable { let id: Int? let name: String let email: String?

init(id: Int? = nil, name: String, email: String? = nil) {
    self.id = id
    self.name = name
    self.email = email
}

}

let db = try Connection(NSHomeDirectory() + "/dbtest.sqlite")

let person = Table("person") let id = Expression("id") let name = Expression("name") let email = Expression<String?>("email")

let createTable = person.create(ifNotExists: true) { t in t.column(id, primaryKey: true) t.column(name) t.column(email) }

try db.run(createTable)

let person1 = Person(name: "John", email: "john@doe.com") let person2 = Person(name: "David", email: nil) let personsArray = [person1, person2]

do { let insert = try person.insertMany(personsArray) try db.run(insert) } catch { print("Error (error.localizedDescription)") }



Thanks in advance.
withzombies commented 1 year ago

Would it be possible to cut a new release with this change?

nathanfallet commented 1 year ago

@withzombies Going to do it tomorrow, I add a reminder on my phone. I'm sorry for this!

withzombies commented 1 year ago

No worries! I appreciate the work you're doing here

jberkel commented 1 year ago

@NathanFallet please undo the merge of the unfinished PR before releasing

nathanfallet commented 1 year ago

Well, we'll wait for #1148 to be merged and then do a release