Electron100 / butane

An ORM for Rust with a focus on simplicity and on writing Rust, not SQL
Apache License 2.0
83 stars 11 forks source link

Composite primary keys #185

Open jayvdb opened 5 months ago

jayvdb commented 5 months ago

I believe that we can rather easily implement composite primary keys without adding any new attributes, if we put the composite key into a separate struct, e.g.

pub struct OrderPrimaryKey {
    pub order_id: u64,
    pub product_id: u64,
}

#[model]
#[derive(Deserialize, Serialize)]
pub struct Order {
    #[serde(flatten)]
    #[pk]
    pub pk: OrderPrimaryKey,
    pub quantity: u64,
    pub price: Decimal,
}

Then the two fields of OrderPrimaryKey would become columns of the Order table. #[pk] would need to do a bit more work to support this, and we may need to put #[derive(FieldType)] on OrderPrimaryKey so it can do some additional preparation work.