ITADN

Inserting `Draft` fails when no `id` is provided, but SCHEMA defines default.

#199Openconnor-ricks 创建于 2025-10-05
bug
C
connor-rickscommented
### Description Given I have a table and schema defined like so... **Table** ```swift @Table("examples") public struct Example: Sendable, Identifiable { @Column(primaryKey: true) public var id: Tagged<Self, UUID> public var name: String } ``` **Schema** ```swift try #sql(""" CREATE TABLE "examples" ( "id" TEXT PRIMARY KEY NOT NULL DEFAULT (someUUIDFunction()), "name" TEXT NOT NULL, ) STRICT """) .execute(database) ``` I would expect that when inserting a `Examples.Draft`, without an `id`, into the database, that the value would be derived from the `someUUIDFunction()` registered to the `db`. Instead, an error is thrown. > Caught error: SQLite error 19: NOT NULL constraint failed: examples.id - while executing `INSERT INTO "examples" ("id", "name") VALUES (NULL, ?) ON CONFLICT ("id") DO UPDATE SET "name" = "excluded"."name"` This is likely because the actual statement passes the `id` column and `NULL` value explicitly, whereas, I'd expect that if the `id` is `nil` in the draft, that the select statement would forego defining the `id` and its value explicitly, giving SQLite an opportunity to field the default value. ### Checklist - [x] I have determined whether this bug is also reproducible in a vanilla SwiftUI project. - [x] If possible, I've reproduced the issue using the `main` branch of this package. - [x] This issue hasn't been addressed in an [existing GitHub issue](https://github.com/pointfreeco/swift-structured-queries/issues) or [discussion](https://github.com/pointfreeco/swift-structured-queries/discussions). ### Expected behavior A draft if correctly inserted into the database. ### Actual behavior An error is thrown > Caught error: SQLite error 19: NOT NULL constraint failed: examples.id - while executing INSERT INTO "examples" ("id", "name") VALUES (NULL, ?) ON CONFLICT ("id") DO UPDATE SET "name" = "excluded"."name" ### Reproducing project Run the one unit test. [DraftDemo.zip](https://github.com/user-attachments/files/22710224/DraftDemo.zip) ### Structured Queries version information 0.22.0 ### Destination operating system iOS 26 ### Xcode version information Xcode 26 ### Swift Compiler version information ```shell Swift 6.1 ```
1 条评论