Quellcode durchsuchen

Add foreign key constraints to EntityID fields.

Kestrel vor 7 Monaten
Ursprung
Commit
221cd7b76a
2 geänderte Dateien mit 12 neuen und 2 gelöschten Zeilen
  1. 6 0
      microrm/src/schema/build.rs
  2. 6 2
      microrm/src/schema/collect.rs

+ 6 - 0
microrm/src/schema/build.rs

@@ -151,6 +151,12 @@ pub(crate) fn collect_from_database<DB: Database>() -> DatabaseSchema {
                     fkey: None,
                     unique: part.unique,
                 }),
+                PartType::IDReference(entity_name) => table.columns.push(ColumnInfo {
+                    name: part.name,
+                    ty: "int".into(),
+                    fkey: Some(format!("`{}`(`id`)", entity_name)),
+                    unique: part.unique,
+                }),
                 PartType::AssocDomain {
                     table_name: assoc_table_name,
                     range_name,

+ 6 - 2
microrm/src/schema/collect.rs

@@ -6,7 +6,12 @@ use crate::schema::{DatumDiscriminator, Relation};
 
 #[derive(Debug)]
 pub enum PartType {
+    /// stores sql data type
     Datum(&'static str),
+    /// stores the entity name
+    IDReference(
+        &'static str,
+    ),
     AssocDomain {
         table_name: String,
         range_name: &'static str,
@@ -33,8 +38,7 @@ impl PartState {
         }
         impl<EP: EntityPart> DatumDiscriminator for Discriminator<EP> {
             fn visit_entity_id<E: Entity>(&mut self) {
-                // TODO: add foreign key constraint
-                self.ty = Some(PartType::Datum(<E::ID as Datum>::sql_type()));
+                self.ty = Some(PartType::IDReference(E::entity_name()));
             }
 
             fn visit_bare_field<T: Datum>(&mut self) {