-
-
Notifications
You must be signed in to change notification settings - Fork 692
Entity generation misinterprets partial unique Index as fully unique #2666
Copy link
Copy link
Closed
Copy link
Labels
Area:code-genArea: code generationArea: code generation
Description
Summary
When generating entities using sea-orm-cli generate entity, if a table has a partial unique index on a single column, that column is incorrectly interpreted as globally unique.
This results in incorrect entity annotations and relationship inference, such as:
- The column is marked as #[sea_orm(unique)], even though it’s only conditionally unique.
- The generator infers a has_one relationship instead of the correct has_many.
Example
Consider the following abstract schema:
CREATE TABLE B (
id SERIAL PRIMARY KEY,
a_id INTEGER NOT NULL,
flag BOOLEAN NOT NULL DEFAULT FALSE,
-- 1. Normal unique index
UNIQUE (a_id, flag),
-- 2. Partial unique index
UNIQUE (a_id) WHERE flag = TRUE
);The second constraint is a partial unique index. sea orm cli currently treats this as if the column a_id were fully unique, resulting in the following incorrect code:
// Incorrect: a_id is not globally unique, only under a condition
#[sea_orm(unique)]
pub a_id: i32,
pub flag: bool,// Generated relation of table A
pub enum Relation {
// Here should be has_many
#[sea_orm(has_one = "super::a::Entity")]
B,
}Environment
Sea orm cli: 1.1.10
Database: PostgreSQL 17
Feature Request:Infer Conditional has_one from Partial Unique Index
In addition to avoiding incorrect #[sea_orm(unique)] attributes, it would be useful if Sea-ORM could detect common patterns involving partial unique indexes with flags and generate relation for it, such as:
UNIQUE (a_id) WHERE flag = TRUEpub enum Relation {
#[sea_orm(has_many = "super::a::Entity")]
B,
#[sea_orm(has_one = "super::a::Entity")]
BAndFlag
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Area:code-genArea: code generationArea: code generation