Skip to content

Database is not recreated in Xcode Previews #449

Description

@pro100filipp

Description

When using it in preview, it seems the database is preserved between preview runs, not recreated anew. It clearly exists in memory (not on disk), and the context shows we are inside the preview.

This is evident because when the preview is run again, the console shows a unique constraint violation error on the table. In other words, the database already exists and contains the corresponding record.

SQLite error 19: UNIQUE constraint failed: examples.id - while executing `INSERT INTO "examples" ("id", "name") VALUES (?, ?)`

Checklist

  • I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
  • I have determined whether this bug is also reproducible in a vanilla GRDB project.
  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

I expect that, similar to SwiftData, if we use an in-memory database, it will be recreated each time we run the preview.

Actual behavior

The database exists and is not recreated.

Reproducing project

SqliteDataPreview.zip

SQLiteData version information

1.6.1

Sharing version information

2.8.0

GRDB version information

7.10.0

Destination operating system

iOS 26

Xcode version information

Version 26.4

Swift Compiler version information

swift-driver version: 1.148.6 Apple Swift version 6.3 (swiftlang-6.3.0.123.5 clang-2100.0.123.102)
Target: arm64-apple-macosx26.0

Activity

  1. hiSandog commented on Jul 10, 2026

    @hiSandog

    An in-memory SQLite database survives for as long as its connection is retained, and Xcode can rerun a preview without restarting the preview process, so tying reset behavior only to XCODE_RUNNING_FOR_PREVIEWS may still be ambiguous. It would help to establish whether the package promises a fresh database per preview process, per preview render, or per dependency construction. A regression test that constructs the preview dependency twice and performs the same unique insert would make the desired lifetime explicit without changing normal in-memory database semantics.

  2. mbrandonw commented on Jul 20, 2026

    @mbrandonw
    Member

    Hi @pro100filipp, this is just how previews work. They can execute multiple times in the same process. To work around this fact you can use stable IDs for all of your seed data and perform upsets instead of inserts.

    Since this isn't an issue with the library I am going to convert it to a discussion. Please feel free to continue the conversation over there!

  3. locked and limited conversation to collaborators on Jul 20, 2026
  4. converted this issue into a discussion #503 on Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions