Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
duhnnie opened this issue Apr 28, 2022 · 5 comments · Fixed by #1138
Closed

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

duhnnie opened this issue Apr 28, 2022 · 5 comments · Fixed by #1138

Comments

@duhnnie
Copy link

duhnnie commented Apr 28, 2022

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

  • SQLite.swift v.0.13.3, revision: 4d543d8
  • Xcode 13.3.1. macOS Big Sur 11.6.5 / macOS Monterey 12.3.1
  • How do do you integrate SQLite.swift in your project?
    • Swift Package manager

General guidelines

import SQLite
import Foundation

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<Int64>("id")
let name = Expression<String>("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
Copy link

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

@nathanfallet
Copy link
Collaborator

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

@withzombies
Copy link

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

@jberkel
Copy link
Collaborator

jberkel commented Sep 10, 2022

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

@nathanfallet
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants