Skip to content

Validate model key and autoincrement annotations #56

Description

@alexeichhorn

Problem

The model macro accepts contradictory or unsafe key declarations:

  • #[key] Option<T> compiles even though a PostgreSQL primary key is always non-null.
  • by_id(None) and active update/delete through a nullable key are generated.
  • #[autoincrement] without #[key] compiles despite inconsistent generated insert behavior.
  • Floating-point #[key] fields use ordinary Rust equality during relation grouping. Rust considers NaN != NaN, while PostgreSQL deliberately treats floating-point NaN values as equal for indexes and ordering.

A nullable pseudo-key can be especially unsafe when the underlying column is not actually unique: eq(None) becomes IS NULL, so generated update/delete operations may match multiple rows.

Expected behavior

  • Reject nullable #[key] fields during macro expansion.
  • Reject #[autoincrement] unless the same field is a key.
  • Either reject f32/f64 key fields or implement key equality that matches PostgreSQL; rejection is the simpler default.
  • Add focused compile-fail tests for invalid annotations.
  • Keep nullable foreign keys supported; this restriction applies only to fields marked #[key].

PostgreSQL primary keys are unique and non-null

Activity

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

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions