Skip to content

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

@duhnnie

Description

@duhnnie

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: "[email protected]")
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions