Models and AST (`model!`)

Generation & ModelSchema

Code generated by model!(...)

After parsing the AST, the macro generates, among other things:

  • a schema() -> ModelSchema function
  • the SeaORM model (generated code)
  • associated relations

The generated schema() function follows this pattern:

pub fn schema() -> runique::migration::schema::ModelSchema {
    runique::migration::ModelSchema::new("User")
        .table_name("users")
        // pk, columns, FK, relations, meta...
        .build()
        .unwrap()
}

Role of `ModelSchema`

ModelSchema is the structural source of truth (table, PK, columns, FK, relations, indexes).

Important runtime methods

  • to_migration(): generates the migration statement
  • fill_form(form, fields, exclude): populates a form from the schema

fill_form behavior

  • the primary key is always excluded,
  • if fields is provided: it acts as a whitelist (order preserved),
  • otherwise exclude acts as a blacklist.