umbral-core 0.0.2

umbral internals: ORM, migrations, routing, DB backends, the Plugin trait. Do not depend on this directly; use the `umbral` facade.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
//! Gap 14 — ForeignKey<T> field type.
//!
//! Coverage:
//!
//! - **Derive classification.** `ForeignKey<User>` on a model produces a
//!   `SqlType::ForeignKey` field spec with the correct `fk_target`.
//! - **Column constant.** The sibling module exposes a `ForeignKeyCol`
//!   constant, and `.eq(1)` produces a well-formed predicate.
//! - **DDL rendering — SQLite.** `render_operation_for` against `"sqlite"`
//!   emits `REFERENCES "user"("id")`.
//! - **DDL rendering — Postgres.** Same but against `"postgres"`.
//! - **Live SQLite round-trip.** Insert a User, insert a Post referencing
//!   it, call `post.author.resolve(&pool)` and get the User back.

#![allow(dead_code)]

use sqlx::SqlitePool;
use umbral::migrate::{Column, Operation, render_operation_for};
use umbral::orm::{ForeignKey, Model, SqlType};

// =========================================================================
// Model declarations
// =========================================================================

#[derive(
    Debug, Clone, PartialEq, sqlx::FromRow, serde::Serialize, serde::Deserialize, umbral::orm::Model,
)]
#[umbral(table = "fk_user")]
pub struct User {
    pub id: i64,
    pub name: String,
}

#[derive(Debug, Clone, sqlx::FromRow, serde::Serialize, serde::Deserialize, umbral::orm::Model)]
#[umbral(table = "fk_post")]
pub struct Post {
    pub id: i64,
    pub title: String,
    pub author: ForeignKey<User>,
}

// =========================================================================
// Derive classification
// =========================================================================

/// `ForeignKey<User>` on Post classifies as `SqlType::ForeignKey` and carries
/// the referenced table name in `fk_target`.
#[test]
fn derive_classifies_foreign_key_sqltype() {
    let by_name: std::collections::HashMap<&str, &umbral::orm::FieldSpec> = <Post as Model>::FIELDS
        .iter()
        .map(|f| (f.name, f))
        .collect();

    let author_field = by_name.get("author").expect("author field must exist");
    assert_eq!(
        author_field.ty,
        SqlType::ForeignKey,
        "ForeignKey<User> should classify as SqlType::ForeignKey"
    );
    assert_eq!(
        author_field.fk_target,
        Some("fk_user"),
        "fk_target should be User::TABLE"
    );
    assert!(
        !author_field.nullable,
        "non-Option ForeignKey is not nullable"
    );
    assert!(!author_field.primary_key, "FK field is not the primary key");
}

// =========================================================================
// Column constant
// =========================================================================

/// `post::AUTHOR.eq(1)` should compile and produce a predicate — that the
/// derive emitted a `ForeignKeyCol` constant for the `author` field.
///
/// The sibling module is named after the struct (`post`), not the table
/// (`fk_post`). The `#[derive(Model)]` macro uses `to_snake_case(struct_name)`.
#[test]
fn foreign_key_col_constant_eq_compiles() {
    // `post` is the sibling module emitted for the `Post` struct.
    use post::AUTHOR;
    let qs = Post::objects().filter(AUTHOR.eq(1));
    let sql = qs.to_sql();
    assert!(
        sql.contains("author"),
        "WHERE clause should reference `author`; got: {sql}"
    );
}

/// `post::AUTHOR.in_(&[1, 2])` compiles and includes the column name.
#[test]
fn foreign_key_col_constant_in_compiles() {
    use post::AUTHOR;
    let qs = Post::objects().filter(AUTHOR.in_(&[1i64, 2i64]));
    let sql = qs.to_sql();
    assert!(
        sql.contains("author"),
        "IN predicate should reference `author`; got: {sql}"
    );
}

// =========================================================================
// DDL rendering
// =========================================================================

/// SQLite `CreateTable` for a model with a ForeignKey column should include
/// `REFERENCES "fk_user"("id")` in the rendered DDL.
#[test]
fn create_table_emits_references_sqlite() {
    let op = Operation::CreateTable {
        table: "fk_post".to_string(),
        columns: vec![
            Column {
                name: "id".to_string(),
                ty: SqlType::BigInt,
                primary_key: true,
                nullable: false,
                fk_target: None,
                noform: false,
                db_constraint: true,
                noedit: false,
                is_string_repr: false,
                max_length: 0,
                choices: Vec::new(),
                choice_labels: Vec::new(),
                default: String::new(),
                is_multichoice: false,
                unique: false,
                on_delete: umbral_core::orm::FkAction::NoAction,
                on_update: umbral_core::orm::FkAction::NoAction,
                index: false,
                auto_now_add: false,
                auto_now: false,
                help: String::new(),
                example: String::new(),
                widget: None,
                supported_backends: Vec::new(),
                min: None,
                max: None,
                text_format: ::core::option::Option::None,
                slug_from: ::core::option::Option::None,
            },
            Column {
                name: "title".to_string(),
                ty: SqlType::Text,
                primary_key: false,
                nullable: false,
                fk_target: None,
                noform: false,
                db_constraint: true,
                noedit: false,
                is_string_repr: false,
                max_length: 0,
                choices: Vec::new(),
                choice_labels: Vec::new(),
                default: String::new(),
                is_multichoice: false,
                unique: false,
                on_delete: umbral_core::orm::FkAction::NoAction,
                on_update: umbral_core::orm::FkAction::NoAction,
                index: false,
                auto_now_add: false,
                auto_now: false,
                help: String::new(),
                example: String::new(),
                widget: None,
                supported_backends: Vec::new(),
                min: None,
                max: None,
                text_format: ::core::option::Option::None,
                slug_from: ::core::option::Option::None,
            },
            Column {
                name: "author".to_string(),
                ty: SqlType::ForeignKey,
                primary_key: false,
                nullable: false,
                fk_target: Some("fk_user".to_string()),
                noform: false,
                db_constraint: true,
                noedit: false,
                is_string_repr: false,
                max_length: 0,
                choices: Vec::new(),
                choice_labels: Vec::new(),
                default: String::new(),
                is_multichoice: false,
                unique: false,
                on_delete: umbral_core::orm::FkAction::NoAction,
                on_update: umbral_core::orm::FkAction::NoAction,
                index: false,
                auto_now_add: false,
                auto_now: false,
                help: String::new(),
                example: String::new(),
                widget: None,
                supported_backends: Vec::new(),
                min: None,
                max: None,
                text_format: ::core::option::Option::None,
                slug_from: ::core::option::Option::None,
            },
        ],
        unique_together: Vec::new(),
        indexes: Vec::new(),
    };

    let stmts = render_operation_for(&op, "sqlite");
    // FK auto-indexing emits a CREATE INDEX alongside the CreateTable;
    // assert on the CREATE TABLE statement itself.
    let sql = stmts
        .iter()
        .find(|s| s.to_ascii_uppercase().contains("CREATE TABLE"))
        .expect("a CREATE TABLE statement");
    let lower = sql.to_ascii_lowercase();

    assert!(
        lower.contains("references"),
        "SQLite DDL should contain REFERENCES; got: {sql}"
    );
    assert!(
        lower.contains("\"fk_user\""),
        "SQLite DDL should reference `fk_user`; got: {sql}"
    );
    assert!(
        lower.contains("(\"id\")"),
        "SQLite DDL should reference column `id`; got: {sql}"
    );
    assert!(
        lower.contains("bigint"),
        "FK column should be BIGINT; got: {sql}"
    );
}

/// Postgres `CreateTable` for a model with a ForeignKey column should
/// include `REFERENCES "fk_user"("id")` in the rendered DDL.
#[test]
fn create_table_emits_references_postgres() {
    let op = Operation::CreateTable {
        table: "fk_post".to_string(),
        columns: vec![
            Column {
                name: "id".to_string(),
                ty: SqlType::BigInt,
                primary_key: true,
                nullable: false,
                fk_target: None,
                noform: false,
                db_constraint: true,
                noedit: false,
                is_string_repr: false,
                max_length: 0,
                choices: Vec::new(),
                choice_labels: Vec::new(),
                default: String::new(),
                is_multichoice: false,
                unique: false,
                on_delete: umbral_core::orm::FkAction::NoAction,
                on_update: umbral_core::orm::FkAction::NoAction,
                index: false,
                auto_now_add: false,
                auto_now: false,
                help: String::new(),
                example: String::new(),
                widget: None,
                supported_backends: Vec::new(),
                min: None,
                max: None,
                text_format: ::core::option::Option::None,
                slug_from: ::core::option::Option::None,
            },
            Column {
                name: "title".to_string(),
                ty: SqlType::Text,
                primary_key: false,
                nullable: false,
                fk_target: None,
                noform: false,
                db_constraint: true,
                noedit: false,
                is_string_repr: false,
                max_length: 0,
                choices: Vec::new(),
                choice_labels: Vec::new(),
                default: String::new(),
                is_multichoice: false,
                unique: false,
                on_delete: umbral_core::orm::FkAction::NoAction,
                on_update: umbral_core::orm::FkAction::NoAction,
                index: false,
                auto_now_add: false,
                auto_now: false,
                help: String::new(),
                example: String::new(),
                widget: None,
                supported_backends: Vec::new(),
                min: None,
                max: None,
                text_format: ::core::option::Option::None,
                slug_from: ::core::option::Option::None,
            },
            Column {
                name: "author".to_string(),
                ty: SqlType::ForeignKey,
                primary_key: false,
                nullable: false,
                fk_target: Some("fk_user".to_string()),
                noform: false,
                db_constraint: true,
                noedit: false,
                is_string_repr: false,
                max_length: 0,
                choices: Vec::new(),
                choice_labels: Vec::new(),
                default: String::new(),
                is_multichoice: false,
                unique: false,
                on_delete: umbral_core::orm::FkAction::NoAction,
                on_update: umbral_core::orm::FkAction::NoAction,
                index: false,
                auto_now_add: false,
                auto_now: false,
                help: String::new(),
                example: String::new(),
                widget: None,
                supported_backends: Vec::new(),
                min: None,
                max: None,
                text_format: ::core::option::Option::None,
                slug_from: ::core::option::Option::None,
            },
        ],
        unique_together: Vec::new(),
        indexes: Vec::new(),
    };

    let stmts = render_operation_for(&op, "postgres");
    // FK auto-indexing emits a CREATE INDEX alongside the CreateTable;
    // assert on the CREATE TABLE statement itself.
    let sql = stmts
        .iter()
        .find(|s| s.to_ascii_uppercase().contains("CREATE TABLE"))
        .expect("a CREATE TABLE statement");
    let lower = sql.to_ascii_lowercase();

    assert!(
        lower.contains("references"),
        "Postgres DDL should contain REFERENCES; got: {sql}"
    );
    assert!(
        lower.contains("\"fk_user\""),
        "Postgres DDL should reference `fk_user`; got: {sql}"
    );
    assert!(
        lower.contains("(\"id\")"),
        "Postgres DDL should reference column `id`; got: {sql}"
    );
    assert!(
        lower.contains("bigint"),
        "FK column should be BIGINT on Postgres; got: {sql}"
    );
}

// =========================================================================
// Live SQLite round-trip
// =========================================================================

async fn fresh_pool() -> SqlitePool {
    let pool = umbral_core::db::connect_sqlite("sqlite::memory:")
        .await
        .expect("in-memory SQLite should always connect");

    // User table — no FK, plain PK.
    sqlx::query(
        "CREATE TABLE fk_user (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL
        )",
    )
    .execute(&pool)
    .await
    .expect("CREATE TABLE fk_user");

    // Post table — FK to fk_user.
    sqlx::query(
        "CREATE TABLE fk_post (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            title TEXT NOT NULL,
            author INTEGER NOT NULL REFERENCES fk_user(id)
        )",
    )
    .execute(&pool)
    .await
    .expect("CREATE TABLE fk_post");

    pool
}

/// Insert a User row and a Post row; verify that `post.author.resolve(&pool)`
/// returns the correct User.
#[tokio::test]
async fn resolve_returns_referenced_user() {
    let pool = fresh_pool().await;

    // Insert a user.
    let user: User = sqlx::query_as::<sqlx::Sqlite, User>(
        "INSERT INTO fk_user (name) VALUES ('Alice') RETURNING id, name",
    )
    .fetch_one(&pool)
    .await
    .expect("insert user");

    // Insert a post referencing the user.
    let post: Post = sqlx::query_as::<sqlx::Sqlite, Post>(
        "INSERT INTO fk_post (title, author) VALUES ('Hello', ?) RETURNING id, title, author",
    )
    .bind(user.id)
    .fetch_one(&pool)
    .await
    .expect("insert post");

    assert_eq!(
        post.author.id(),
        user.id,
        "post.author.id() should equal user.id"
    );

    // Resolve through the ForeignKey.
    let resolved = post
        .author
        .resolve(&pool)
        .await
        .expect("resolve should succeed");

    assert_eq!(resolved.id, user.id, "resolved.id should match");
    assert_eq!(resolved.name, "Alice", "resolved.name should match");
}

/// `ForeignKey::new(pk)` and `ForeignKey::id()` round-trip correctly.
/// The post-generic-FK API replaces the `From<i64>` shorthand with
/// the explicit `new(...)` constructor so non-i64 PK types (String,
/// UUID) work the same way.
#[test]
fn foreign_key_from_and_id_roundtrip() {
    let fk: ForeignKey<User> = ForeignKey::new(42i64);
    assert_eq!(fk.id(), 42);
}

/// `ForeignKey::set()` updates the stored value.
#[test]
fn foreign_key_set_updates_value() {
    let mut fk: ForeignKey<User> = ForeignKey::new(1i64);
    fk.set(99);
    assert_eq!(fk.id(), 99);
}

/// `ForeignKey<T>` serialises as a plain JSON integer for an
/// i64-keyed target. (String-keyed targets serialise as a JSON
/// string; UUID-keyed as a UUID-shaped string.)
#[test]
fn foreign_key_serialises_as_integer() {
    let fk: ForeignKey<User> = ForeignKey::new(7i64);
    let json = serde_json::to_string(&fk).unwrap();
    assert_eq!(json, "7", "ForeignKey should serialise as a bare integer");
}

/// `ForeignKey<T>` deserialises from a plain JSON integer.
#[test]
fn foreign_key_deserialises_from_integer() {
    let fk: ForeignKey<User> = serde_json::from_str("42").unwrap();
    assert_eq!(fk.id(), 42);
}

// =========================================================================
// Gap #69: self-referential foreign keys.
//
// Some frameworks use a string sentinel for self-references
// because the model class isn't bound yet when the field is declared.
// Rust solves the same problem differently: `ForeignKey<T>` stores
// `T::PrimaryKey` and `Option<Box<T>>` (boxed so the type stays
// finite-size), and `<T as Model>::TABLE` resolves at the same
// expansion step that emits `impl Model for Self`. Net result: writing
// `ForeignKey<MyType>` *inside* `MyType` Just Works — no `Self`
// keyword needed, no string sentinel, no macro special-case.
// =========================================================================

/// A category tree: each row optionally points at its parent, with
/// `ON DELETE CASCADE` so deleting a root prunes the subtree. The
/// `parent_id` column's `fk_target` resolves to the *same table* the
/// model lives in.
#[derive(Debug, Clone, sqlx::FromRow, serde::Serialize, serde::Deserialize, umbral::orm::Model)]
#[umbral(table = "fk_category")]
pub struct Category {
    pub id: i64,
    pub name: String,
    #[umbral(on_delete = "cascade")]
    pub parent_id: Option<ForeignKey<Category>>,
}

/// Static metadata: the self-FK column carries `fk_target =
/// Some("fk_category")` — the model's own table name. The derive
/// emits `<Category as Model>::TABLE` for the target lookup, which
/// resolves to the const that the same derive expansion is producing
/// alongside the field metadata. The const resolution happens before
/// the const's value is needed, which is fine because const-eval
/// runs after the expansion is complete.
#[test]
fn self_referential_fk_target_resolves_to_own_table() {
    let by_name: std::collections::HashMap<&str, &umbral::orm::FieldSpec> =
        <Category as Model>::FIELDS
            .iter()
            .map(|f| (f.name, f))
            .collect();
    let parent = by_name.get("parent_id").expect("parent_id field present");
    assert_eq!(parent.ty, SqlType::ForeignKey);
    assert_eq!(parent.fk_target, Some("fk_category"));
    assert!(parent.nullable, "Option<ForeignKey<T>> should be nullable");
    assert!(
        matches!(parent.on_delete, umbral::orm::FkAction::Cascade),
        "ON DELETE CASCADE should round-trip from the attribute"
    );
}

/// The DDL renders `REFERENCES "fk_category"("id") ON DELETE CASCADE`
/// for the parent column — the table is self-referencing within a
/// single CREATE TABLE statement. Both SQLite and Postgres accept
/// that shape because the column constraint is evaluated against
/// the table being created in the same statement.
#[test]
fn self_referential_fk_renders_inline_references() {
    let meta = umbral::migrate::ModelMeta::for_::<Category>();
    let op = Operation::CreateTable {
        table: Category::TABLE.to_string(),
        columns: meta.fields.clone(),
        unique_together: Vec::new(),
        indexes: Vec::new(),
    };
    for backend in ["sqlite", "postgres"] {
        let sql = render_operation_for(&op, backend).join("\n");
        assert!(
            sql.contains("REFERENCES \"fk_category\"(\"id\")"),
            "{backend}: expected self-reference in REFERENCES tail; got: {sql}"
        );
        assert!(
            sql.to_uppercase().contains("ON DELETE CASCADE"),
            "{backend}: expected ON DELETE CASCADE; got: {sql}"
        );
    }
}

/// End-to-end against a live SQLite engine: create the table, insert
/// a root + a child, assert the FK link, then delete the root and
/// confirm the cascade pruned the child. The same `Operation::
/// CreateTable` the migration engine emits is the only DDL run —
/// nothing is hand-stitched.
#[tokio::test]
async fn self_referential_fk_round_trips_through_sqlite_with_cascade() {
    let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
    sqlx::query("PRAGMA foreign_keys = ON;")
        .execute(&pool)
        .await
        .unwrap();
    let meta = umbral::migrate::ModelMeta::for_::<Category>();
    let op = Operation::CreateTable {
        table: Category::TABLE.to_string(),
        columns: meta.fields.clone(),
        unique_together: Vec::new(),
        indexes: Vec::new(),
    };
    for stmt in render_operation_for(&op, "sqlite") {
        sqlx::query(&stmt).execute(&pool).await.unwrap();
    }
    sqlx::query("INSERT INTO fk_category (id, name, parent_id) VALUES (1, 'root', NULL)")
        .execute(&pool)
        .await
        .unwrap();
    sqlx::query("INSERT INTO fk_category (id, name, parent_id) VALUES (2, 'child', 1)")
        .execute(&pool)
        .await
        .unwrap();
    let (children,): (i64,) =
        sqlx::query_as("SELECT COUNT(*) FROM fk_category WHERE parent_id = 1")
            .fetch_one(&pool)
            .await
            .unwrap();
    assert_eq!(children, 1, "child should reference the root");

    sqlx::query("DELETE FROM fk_category WHERE id = 1")
        .execute(&pool)
        .await
        .unwrap();
    let (remaining,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM fk_category")
        .fetch_one(&pool)
        .await
        .unwrap();
    assert_eq!(
        remaining, 0,
        "CASCADE should have pruned the child after the root was deleted"
    );
}

// =========================================================================
// BUG-15 from bugs/tests/testBugs.md: a OneToOne relationship is
// expressible today as `#[umbral(unique)] ForeignKey<T>`. The
// emitted DDL combines UNIQUE + REFERENCES; the second INSERT
// pointing at the same target row fails the UNIQUE constraint.
// No dedicated `OneToOne<T>` type is needed at v1; this test
// pins the pattern so future macro refactors don't silently
// drop the combination.
// =========================================================================

#[derive(Debug, Clone, sqlx::FromRow, serde::Serialize, serde::Deserialize, umbral::orm::Model)]
#[umbral(table = "fk_profile")]
pub struct Profile {
    pub id: i64,
    /// One profile per user. The `#[umbral(unique)]` flag is what
    /// makes this a 1:1 rather than a 1:N relationship.
    #[umbral(unique, on_delete = "cascade")]
    pub user_id: ForeignKey<User>,
    pub bio: String,
}

#[test]
fn one_to_one_pattern_emits_unique_and_references() {
    let meta = umbral::migrate::ModelMeta::for_::<Profile>();
    let user_id = meta
        .fields
        .iter()
        .find(|c| c.name == "user_id")
        .expect("user_id present");
    assert_eq!(user_id.ty, SqlType::ForeignKey);
    assert_eq!(user_id.fk_target.as_deref(), Some("fk_user"));
    assert!(
        user_id.unique,
        "the #[umbral(unique)] flag is what makes this a 1:1",
    );

    for backend in ["sqlite", "postgres"] {
        let op = Operation::CreateTable {
            table: Profile::TABLE.to_string(),
            columns: meta.fields.clone(),
            unique_together: Vec::new(),
            indexes: Vec::new(),
        };
        let sql = render_operation_for(&op, backend).join("\n");
        assert!(
            sql.to_uppercase().contains("UNIQUE"),
            "{backend}: should emit UNIQUE for the 1:1 column; got: {sql}",
        );
        assert!(
            sql.contains("REFERENCES \"fk_user\"(\"id\")"),
            "{backend}: should still emit the FK REFERENCES clause; got: {sql}",
        );
    }
}

#[tokio::test]
async fn one_to_one_rejects_second_reference_to_same_target() {
    let pool = SqlitePool::connect("sqlite::memory:").await.unwrap();
    sqlx::query("PRAGMA foreign_keys = ON;")
        .execute(&pool)
        .await
        .unwrap();
    // Create the user side by hand for the test (test fixture only).
    sqlx::query("CREATE TABLE fk_user (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)")
        .execute(&pool)
        .await
        .unwrap();
    let meta = umbral::migrate::ModelMeta::for_::<Profile>();
    let op = Operation::CreateTable {
        table: Profile::TABLE.to_string(),
        columns: meta.fields.clone(),
        unique_together: Vec::new(),
        indexes: Vec::new(),
    };
    for stmt in render_operation_for(&op, "sqlite") {
        sqlx::query(&stmt).execute(&pool).await.unwrap();
    }
    sqlx::query("INSERT INTO fk_user (id, name) VALUES (1, 'alice')")
        .execute(&pool)
        .await
        .unwrap();
    sqlx::query("INSERT INTO fk_profile (id, user_id, bio) VALUES (1, 1, 'first')")
        .execute(&pool)
        .await
        .unwrap();
    let dupe = sqlx::query("INSERT INTO fk_profile (id, user_id, bio) VALUES (2, 1, 'second')")
        .execute(&pool)
        .await;
    assert!(
        dupe.is_err(),
        "a second profile pointing at the same user must fail the UNIQUE constraint; succeeded instead",
    );
    let msg = format!("{:?}", dupe.unwrap_err()).to_lowercase();
    assert!(
        msg.contains("unique"),
        "the error should mention the UNIQUE violation; got: {msg}",
    );
}