{"record":{"id":"6e2122ff1ba4ec7b","repo":"transact-rs/sqlx","slug":"failed-to-apply-test-fixture","errorCode":null,"errorMessage":"failed to apply test fixture {:?}: {:?}","messagePattern":"failed to apply test fixture (.+?): (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sqlx-core/src/testing/mod.rs","lineNumber":269,"sourceCode":"    for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,\n{\n    let mut conn = copts\n        .connect()\n        .await\n        .expect(\"failed to connect to test database\");\n\n    if let Some(migrator) = args.migrator {\n        migrator\n            .run_direct(None, &mut conn, false)\n            .await\n            .expect(\"failed to apply migrations\");\n    }\n\n    for fixture in args.fixtures {\n        (&mut conn)\n            .execute(fixture.contents)\n            .await\n            .unwrap_or_else(|e| panic!(\"failed to apply test fixture {:?}: {:?}\", fixture.path, e));\n    }\n\n    conn.close()\n        .await\n        .expect(\"failed to close setup connection\");\n}\n","sourceCodeStart":251,"sourceCodeEnd":276,"githubUrl":"https://github.com/transact-rs/sqlx/blob/03af8bcc5711a1935580a54bea249c219a0c217d/sqlx-core/src/testing/mod.rs#L251-L276","documentation":"sqlx-core's testing scaffold (setup_test_db, used by #[sqlx::test]) panics when executing a fixture SQL file against the freshly created test database fails. The panic wraps the sqlx error from `conn.execute(fixture.contents)`, printing the fixture path and the underlying DB error, so the real cause (bad SQL, dependency order, missing schema) is in the attached error value.","triggerScenarios":"`#[sqlx::test(fixtures(path(\"users.sql\")))]` where users.sql contains SQL that errors on the empty test database: referencing a table created by another fixture that isn't listed, syntax errors, DB-specific SQL, INSERTs violating constraints, or fixtures executed in the wrong order.","commonSituations":"Fixture files that worked locally against a manually-seeded dev DB but assume objects a clean test DB lacks; fixture ordering issues (fixtures run in listed order but dependencies not listed first); migrations not applied before fixtures in the test setup.","solutions":["Read the wrapped error in the panic message — it names the exact SQL error (relation does not exist, constraint violation, syntax).","Ensure all prerequisite fixtures are listed, dependencies first: `fixtures(path(\"schema.sql\"), path(\"users.sql\"))`.","Run migrations before fixtures (the test scaffold applies migrations if configured; otherwise execute them in a setup step).","Validate the fixture SQL directly: pipe it into a fresh `psql`/`mysql`/`sqlite3` empty database to reproduce outside of tests.","Make fixtures idempotent and self-contained (CREATE TABLE IF NOT EXISTS / ON CONFLICT DO NOTHING)."],"exampleFix":"// before\n#[sqlx::test(fixtures(path(\"orders.sql\")))]\nasync fn test_orders(pool: PgPool) { /* orders.sql references users table */ }\n\n// after\n#[sqlx::test(fixtures(path(\"schema.sql\"), path(\"users.sql\"), path(\"orders.sql\")))]\nasync fn test_orders(pool: PgPool) { /* prerequisites loaded first */ }","handlingStrategy":"validation","validationCode":"// Validate fixture SQL against a scratch DB before running tests:\n// psql \"postgres://localhost/scratch\" -v ON_ERROR_STOP=1 -f tests/fixtures/orders.sql\n// Also list fixtures in dependency order:\n// #[sqlx::test(fixtures(path(\"schema.sql\"), path(\"users.sql\"), path(\"orders.sql\")))]","typeGuard":null,"tryCatchPattern":"// The panic already embeds the underlying error; parse it in test output:\n// failed to apply test fixture \"tests/fixtures/orders.sql\": <sqlx error> —\n// read the wrapped sqlx::Error to find the failing SQL statement.","preventionTips":["Keep each fixture self-contained or list prerequisites first","Run migrations before fixtures in test setup","Validate fixture SQL in CI against an empty database","Use IF NOT EXISTS / ON CONFLICT to make fixtures idempotent"],"tags":["testing","fixtures","sql","sqlx"],"backgroundTag":"fixture-setup-failed","analyzedSha":"03af8bcc5711a1935580a54bea249c219a0c217d","analyzedAt":"2026-09-03T15:01:28.752Z","contentChangedAt":"2026-09-03T15:01:28.752Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}