ORM-03: Make the primary key explicit on Model #3

Closed
opened 2026-10-13 12:49:38 +01:00 by NoctisWolfshadow · 0 comments
Owner

Depends on: #1

Context

Model::insert silently treats COLUMNS[0] as the autoincrement key and COLUMNS[1..] as the insertable columns. Relationships, find, update and save all need to know the primary key explicitly, and it should not have to be the first column.

Tasks

  • Add const PRIMARY_KEY: &'static str = "id";, fn id(&self) -> i64 and fn set_id(&mut self, id: i64) to Model.
  • Build the insert column list as COLUMNS minus PRIMARY_KEY (no more [1..]) in a pub(crate) fn insert_sql() -> String.
  • Document the contract: insert_values() returns the non-PK values in COLUMNS order. debug_assert_eq! its length against COLUMNS.len() - 1 when inserting.
  • Document the scope: single integer primary key. Composite or UUID keys are out of scope.
  • Update the User fixture in tests/common.

Example

pub trait Model: Sized {
    const TABLE: &'static str;
    const PRIMARY_KEY: &'static str = "id";
    const COLUMNS: &'static [&'static str];

    fn id(&self) -> i64;
    fn set_id(&mut self, id: i64);

    fn from_row(row: &Row) -> rusqlite::Result<Self>;
    /// Non-PK values, in `COLUMNS` order.
    fn insert_values(&self) -> Vec<Value>;
}

// tests/common/mod.rs
impl Model for User {
    const TABLE: &'static str = "users";
    const COLUMNS: &'static [&'static str] = &["id", "name", "email", "age", "active"];

    fn id(&self) -> i64 { self.id }
    fn set_id(&mut self, id: i64) { self.id = id; }
    // from_row / insert_values unchanged
}

insert_sql is crate-private, so the test lives next to it in src/model.rs:

#[cfg(test)]
mod tests {
    use super::*;

    struct Legacy { name: String, id: i64 } // the PK is last
    impl Model for Legacy {
        const TABLE: &'static str = "legacy";
        const COLUMNS: &'static [&'static str] = &["name", "id"];
        /* id, set_id, from_row, insert_values ... */
    }

    #[test]
    fn insert_sql_skips_the_primary_key_wherever_it_is() {
        assert_eq!(Legacy::insert_sql(), "INSERT INTO legacy (name) VALUES (?)");
    }
}

Acceptance criteria

  • The unit test above passes.
  • Existing tests pass with the updated fixture.
  • Docs state the integer-PK scope.

Notes

#4 adds the ModelState hooks to the same fixtures, so the Legacy test model needs them too once that lands.

**Depends on:** #1 ## Context `Model::insert` silently treats `COLUMNS[0]` as the autoincrement key and `COLUMNS[1..]` as the insertable columns. Relationships, `find`, `update` and `save` all need to know the primary key explicitly, and it should not have to be the first column. ## Tasks - [x] Add `const PRIMARY_KEY: &'static str = "id";`, `fn id(&self) -> i64` and `fn set_id(&mut self, id: i64)` to `Model`. - [x] Build the insert column list as `COLUMNS` minus `PRIMARY_KEY` (no more `[1..]`) in a `pub(crate) fn insert_sql() -> String`. - [x] Document the contract: `insert_values()` returns the non-PK values in `COLUMNS` order. `debug_assert_eq!` its length against `COLUMNS.len() - 1` when inserting. - [x] Document the scope: single integer primary key. Composite or UUID keys are out of scope. - [x] Update the `User` fixture in `tests/common`. ## Example ```rust pub trait Model: Sized { const TABLE: &'static str; const PRIMARY_KEY: &'static str = "id"; const COLUMNS: &'static [&'static str]; fn id(&self) -> i64; fn set_id(&mut self, id: i64); fn from_row(row: &Row) -> rusqlite::Result<Self>; /// Non-PK values, in `COLUMNS` order. fn insert_values(&self) -> Vec<Value>; } // tests/common/mod.rs impl Model for User { const TABLE: &'static str = "users"; const COLUMNS: &'static [&'static str] = &["id", "name", "email", "age", "active"]; fn id(&self) -> i64 { self.id } fn set_id(&mut self, id: i64) { self.id = id; } // from_row / insert_values unchanged } ``` `insert_sql` is crate-private, so the test lives next to it in `src/model.rs`: ```rust #[cfg(test)] mod tests { use super::*; struct Legacy { name: String, id: i64 } // the PK is last impl Model for Legacy { const TABLE: &'static str = "legacy"; const COLUMNS: &'static [&'static str] = &["name", "id"]; /* id, set_id, from_row, insert_values ... */ } #[test] fn insert_sql_skips_the_primary_key_wherever_it_is() { assert_eq!(Legacy::insert_sql(), "INSERT INTO legacy (name) VALUES (?)"); } } ``` ## Acceptance criteria - [x] The unit test above passes. - [x] Existing tests pass with the updated fixture. - [x] Docs state the integer-PK scope. ## Notes #4 adds the `ModelState` hooks to the same fixtures, so the `Legacy` test model needs them too once that lands.
NoctisWolfshadow added reference refactor_to_v0.2.0 2026-10-16 14:49:02 +01:00
Sign in to join this conversation.
No description provided.