diff --git a/psl/psl/tests/attributes/relations/relations_new.rs b/psl/psl/tests/attributes/relations/relations_new.rs index 3ff22edc7c04..b474846f41fe 100644 --- a/psl/psl/tests/attributes/relations/relations_new.rs +++ b/psl/psl/tests/attributes/relations/relations_new.rs @@ -19,18 +19,18 @@ fn relation_happy_path() { let schema = parse(dml); let user_model = schema.assert_has_model("User"); + let post_model = schema.assert_has_model("Post"); user_model .assert_has_relation_field("posts") .assert_arity(&dml::FieldArity::List) - .assert_relation_to("Post") + .assert_relation_to(post_model.id) .assert_relation_base_fields(&[]) .assert_relation_referenced_fields(&[]); - let post_model = schema.assert_has_model("Post"); post_model .assert_has_relation_field("user") .assert_arity(&dml::FieldArity::Required) - .assert_relation_to("User") + .assert_relation_to(user_model.id) .assert_relation_base_fields(&["userId"]) .assert_relation_referenced_fields(&["id"]); } diff --git a/psl/psl/tests/attributes/relations/relations_positive.rs b/psl/psl/tests/attributes/relations/relations_positive.rs index d602521c1b47..5cb9cf5818ce 100644 --- a/psl/psl/tests/attributes/relations/relations_positive.rs +++ b/psl/psl/tests/attributes/relations/relations_positive.rs @@ -40,10 +40,10 @@ fn settings_must_be_deteced() { let schema = parse(dml); - schema - .assert_has_model("Todo") + let todo_model = schema.assert_has_model("Todo"); + todo_model .assert_has_relation_field("parent_todo") - .assert_relation_to("Todo") + .assert_relation_to(todo_model.id) .assert_relation_referenced_fields(&["id"]) .assert_arity(&dml::FieldArity::Optional); } @@ -68,19 +68,21 @@ fn resolve_relation() { let schema = parse(dml); let user_model = schema.assert_has_model("User"); + let post_model = schema.assert_has_model("Post"); user_model .assert_has_scalar_field("firstName") .assert_base_type(&ScalarType::String); user_model .assert_has_relation_field("posts") - .assert_relation_to("Post") + .assert_relation_to(post_model.id) .assert_arity(&dml::FieldArity::List); - let post_model = schema.assert_has_model("Post"); post_model .assert_has_scalar_field("text") .assert_base_type(&ScalarType::String); - post_model.assert_has_relation_field("user").assert_relation_to("User"); + post_model + .assert_has_relation_field("user") + .assert_relation_to(user_model.id); } #[test] @@ -102,10 +104,11 @@ fn resolve_related_field() { let schema = parse(dml); + let user_model = schema.assert_has_model("User"); let post_model = schema.assert_has_model("Post"); post_model .assert_has_relation_field("user") - .assert_relation_to("User") + .assert_relation_to(user_model.id) .assert_relation_referenced_fields(&["firstName"]); } @@ -132,10 +135,11 @@ fn resolve_related_fields() { let schema = parse(dml); + let user_model = schema.assert_has_model("User"); let post_model = schema.assert_has_model("Post"); post_model .assert_has_relation_field("user") - .assert_relation_to("User") + .assert_relation_to(user_model.id) .assert_relation_base_fields(&["authorFirstName", "authorLastName"]) .assert_relation_referenced_fields(&["firstName", "lastName"]); } @@ -162,31 +166,31 @@ fn allow_multiple_relations() { let schema = parse(dml); let user_model = schema.assert_has_model("User"); + let post_model = schema.assert_has_model("Post"); user_model .assert_field_count(3) .assert_has_relation_field("posts") - .assert_relation_to("Post") + .assert_relation_to(post_model.id) .assert_arity(&dml::FieldArity::List) .assert_relation_name("PostToUser"); user_model .assert_has_relation_field("more_posts") - .assert_relation_to("Post") + .assert_relation_to(post_model.id) .assert_arity(&dml::FieldArity::List) .assert_relation_name("more_posts"); - let post_model = schema.assert_has_model("Post"); post_model .assert_field_count(6) .assert_has_scalar_field("text") .assert_base_type(&ScalarType::String); post_model .assert_has_relation_field("user") - .assert_relation_to("User") + .assert_relation_to(user_model.id) .assert_arity(&dml::FieldArity::Required) .assert_relation_name("PostToUser"); post_model .assert_has_relation_field("posting_user") - .assert_relation_to("User") + .assert_relation_to(user_model.id) .assert_arity(&dml::FieldArity::Required) .assert_relation_name("more_posts"); } @@ -209,14 +213,18 @@ fn allow_complicated_self_relations() { let schema = parse(dml); let user_model = schema.assert_has_model("User"); - user_model.assert_has_relation_field("son").assert_relation_to("User"); + user_model + .assert_has_relation_field("son") + .assert_relation_to(user_model.id); user_model .assert_has_relation_field("father") - .assert_relation_to("User"); + .assert_relation_to(user_model.id); user_model .assert_has_relation_field("husband") - .assert_relation_to("User"); - user_model.assert_has_relation_field("wife").assert_relation_to("User"); + .assert_relation_to(user_model.id); + user_model + .assert_has_relation_field("wife") + .assert_relation_to(user_model.id); } #[test] diff --git a/psl/psl/tests/base/array_sugar.rs b/psl/psl/tests/base/array_sugar.rs index 39aceec0b618..bb2097b3300d 100644 --- a/psl/psl/tests/base/array_sugar.rs +++ b/psl/psl/tests/base/array_sugar.rs @@ -18,9 +18,10 @@ fn should_treat_single_values_as_arrays_of_length_one() { let schema = parse(dml); + let user_model = schema.assert_has_model("User"); let post_model = schema.assert_has_model("Post"); post_model .assert_has_relation_field("user") - .assert_relation_to("User") + .assert_relation_to(user_model.id) .assert_relation_referenced_fields(&["id"]); } diff --git a/psl/psl/tests/common/mod.rs b/psl/psl/tests/common/mod.rs index bf5b1fb4773b..fec71ed63f49 100644 --- a/psl/psl/tests/common/mod.rs +++ b/psl/psl/tests/common/mod.rs @@ -5,7 +5,7 @@ pub(crate) use asserts::*; pub use dml::*; pub use expect_test::expect; -use psl::{diagnostics::*, Configuration, StringFromEnvVar}; +use psl::{diagnostics::*, schema_ast::ast, Configuration, StringFromEnvVar}; pub(crate) fn reformat(input: &str) -> String { psl::reformat(input, 2).unwrap_or_else(|| input.to_owned()) @@ -39,7 +39,7 @@ pub(crate) trait CompositeTypeFieldAsserts { pub(crate) trait RelationFieldAsserts { fn assert_relation_name(&self, t: &str) -> &Self; - fn assert_relation_to(&self, t: &str) -> &Self; + fn assert_relation_to(&self, t: ast::ModelId) -> &Self; fn assert_relation_delete_strategy(&self, t: dml::ReferentialAction) -> &Self; fn assert_relation_update_strategy(&self, t: dml::ReferentialAction) -> &Self; fn assert_relation_referenced_fields(&self, t: &[&str]) -> &Self; @@ -191,7 +191,7 @@ impl RelationFieldAsserts for dml::RelationField { self } - fn assert_relation_to(&self, t: &str) -> &Self { + fn assert_relation_to(&self, t: ast::ModelId) -> &Self { assert_eq!(self.relation_info.referenced_model, t); self } diff --git a/query-engine/connectors/sql-query-connector/src/model_extensions/relation.rs b/query-engine/connectors/sql-query-connector/src/model_extensions/relation.rs index 165f2f0007e2..a5428e665cbc 100644 --- a/query-engine/connectors/sql-query-connector/src/model_extensions/relation.rs +++ b/query-engine/connectors/sql-query-connector/src/model_extensions/relation.rs @@ -75,8 +75,7 @@ impl AsTable for Relation { } RelationLinkManifestation::Inline(ref m) => self .internal_data_model() - .find_model(&m.in_table_of_model_name) - .unwrap() + .find_model_by_id(m.in_table_of_model) .as_table(ctx), } } diff --git a/query-engine/dml/src/datamodel.rs b/query-engine/dml/src/datamodel.rs index 1ee44d1a2bf2..3c6e1aff93dd 100644 --- a/query-engine/dml/src/datamodel.rs +++ b/query-engine/dml/src/datamodel.rs @@ -2,13 +2,8 @@ use crate::composite_type::CompositeType; use crate::field::RelationField; use crate::model::Model; use crate::relation_info::RelationInfo; +use psl_core::schema_ast::ast; -/// Entities in the datamodel can be flagged as `is_commented_out`. This lets the renderer -/// know that introspection encountered unsupported names or features and these are supposed -/// to be rendered as comments. Since the parser will not set these flags when reading a schema -/// string, only introspection and the lowering of the datamodel to the ast care about these flags. -/// The FieldType: Unsupported behaves in the same way. -/// Both of these are never converted into the internal datamodel. #[derive(Debug, Default)] pub struct Datamodel { pub models: Vec, @@ -31,6 +26,10 @@ impl Datamodel { self.models().find(|model| model.name == name) } + pub fn find_model_by_id(&self, id: ast::ModelId) -> Option<&Model> { + self.models().find(|m| m.id == id) + } + /// Finds a composite type by name. pub fn find_composite_type(&self, name: &str) -> Option<&CompositeType> { self.composite_types().find(|composite| composite.name == name) @@ -43,11 +42,6 @@ impl Datamodel { .find(|model| model.database_name.as_deref() == Some(db_name)) } - /// Finds parent model for a field reference. - pub fn find_model_by_relation_field_ref(&self, field: &RelationField) -> Option<&Model> { - self.find_model(&self.find_related_field_bang(field).1.relation_info.referenced_model) - } - /// Finds a model by name and returns a mutable reference. pub fn find_model_mut(&mut self, name: &str) -> &mut Model { self.models @@ -56,22 +50,9 @@ impl Datamodel { .expect("We assume an internally valid datamodel before mutating.") } - /// Returns (model_name, field_name) for all relation fields pointing to a specific model. - pub fn find_relation_fields_for_model(&mut self, model_name: &str) -> Vec<(String, String)> { - let mut fields = vec![]; - for model in self.models() { - for field in model.relation_fields() { - if field.relation_info.referenced_model == model_name { - fields.push((model.name.clone(), field.name.clone())) - } - } - } - fields - } - /// Finds a relation field related to a relation info. Returns a tuple (index_of_relation_field_in_model, relation_field). pub fn find_related_field_for_info(&self, info: &RelationInfo, exclude: &str) -> Option<(usize, &RelationField)> { - self.find_model(&info.referenced_model) + self.find_model_by_id(info.referenced_model) .expect("The model referred to by a RelationInfo should always exist.") .fields .iter() diff --git a/query-engine/dml/src/field.rs b/query-engine/dml/src/field.rs index 0a908b53d324..f5732535fcfc 100644 --- a/query-engine/dml/src/field.rs +++ b/query-engine/dml/src/field.rs @@ -331,10 +331,6 @@ impl RelationField { self.emulates_referential_actions = Some(value); } - pub fn points_to_model(&self, name: &str) -> bool { - self.relation_info.referenced_model == name - } - pub fn is_required(&self) -> bool { self.arity.is_required() } @@ -343,14 +339,6 @@ impl RelationField { self.arity.is_list() } - pub fn is_singular(&self) -> bool { - !self.is_list() - } - - pub fn is_optional(&self) -> bool { - self.arity.is_optional() - } - pub fn default_on_delete_action(&self) -> ReferentialAction { use ReferentialAction::*; @@ -446,14 +434,6 @@ impl ScalarField { self.arity.is_required() } - pub fn is_list(&self) -> bool { - self.arity.is_list() - } - - pub fn is_singular(&self) -> bool { - !self.is_list() - } - pub fn is_optional(&self) -> bool { self.arity.is_optional() } diff --git a/query-engine/dml/src/lift.rs b/query-engine/dml/src/lift.rs index 169f657e3083..19169b543365 100644 --- a/query-engine/dml/src/lift.rs +++ b/query-engine/dml/src/lift.rs @@ -120,7 +120,7 @@ impl<'a> LiftAstToDml<'a> { let referenced_model = relation.referenced_model(); - let relation_info = RelationInfo::new(referenced_model.name()); + let relation_info = RelationInfo::new(referenced_model.id); let forward_field_walker = relation.forward_relation_field().unwrap(); // Construct a relation field in the DML for an existing relation field in the source. @@ -181,7 +181,7 @@ impl<'a> LiftAstToDml<'a> { // Back field { - let relation_info = RelationInfo::new(relation.referencing_model().name()); + let relation_info = RelationInfo::new(relation.referencing_model().id); let model = schema.find_model_mut(relation.referenced_model().name()); let mut field = if let Some(relation_field) = relation.back_relation_field() { @@ -227,7 +227,7 @@ impl<'a> LiftAstToDml<'a> { for relation_field in [relation.field_a(), relation.field_b()] { let ast_field = relation_field.ast_field(); let arity = ast_field.arity; - let relation_info = RelationInfo::new(relation_field.related_model().name()); + let relation_info = RelationInfo::new(relation_field.related_model().id); let referential_arity = relation_field.referential_arity(); let mut field = RelationField::new( relation_field.id, @@ -262,7 +262,7 @@ impl<'a> LiftAstToDml<'a> { for relation_field in [relation.field_a(), relation.field_b()] { let ast_field = relation_field.ast_field(); let arity = ast_field.arity; - let relation_info = RelationInfo::new(relation_field.related_model().name()); + let relation_info = RelationInfo::new(relation_field.related_model().id); let referential_arity = relation_field.referential_arity(); let mut field = RelationField::new( diff --git a/query-engine/dml/src/relation_info.rs b/query-engine/dml/src/relation_info.rs index 42352641bee0..a2bac800d157 100644 --- a/query-engine/dml/src/relation_info.rs +++ b/query-engine/dml/src/relation_info.rs @@ -1,10 +1,10 @@ -use psl_core::parser_database as db; +use psl_core::{parser_database as db, schema_ast::ast}; /// Holds information about a relation field. #[derive(Debug, PartialEq, Clone)] pub struct RelationInfo { /// The target model of the relation. - pub referenced_model: String, + pub referenced_model: ast::ModelId, /// The fields forming the relation. pub fields: Vec, /// The target field of the relation a.k.a. `references` @@ -24,9 +24,9 @@ pub struct RelationInfo { impl RelationInfo { /// Creates a new relation info for the /// given target model. - pub fn new(model: &str) -> RelationInfo { + pub fn new(referenced_model: ast::ModelId) -> RelationInfo { RelationInfo { - referenced_model: String::from(model), + referenced_model, fields: Vec::new(), references: Vec::new(), name: String::new(), diff --git a/query-engine/prisma-models/src/builders/internal_dm_builder.rs b/query-engine/prisma-models/src/builders/internal_dm_builder.rs index b0dffb62426e..e91ac3e2d633 100644 --- a/query-engine/prisma-models/src/builders/internal_dm_builder.rs +++ b/query-engine/prisma-models/src/builders/internal_dm_builder.rs @@ -8,7 +8,7 @@ use crate::{ IndexType, InlineRelation, RelationLinkManifestation, RelationSide, RelationTable, TypeIdentifier, }; use dml::{self, CompositeTypeFieldType, Datamodel, Ignorable, WithDatabaseName}; -use psl::datamodel_connector::RelationMode; +use psl::{datamodel_connector::RelationMode, schema_ast::ast}; pub(crate) fn model_builders( datamodel: &Datamodel, @@ -19,6 +19,7 @@ pub(crate) fn model_builders( .filter(|model| !model.is_ignored()) .filter(|model| model.is_supported()) .map(|model| ModelBuilder { + id: model.id, name: model.name.clone(), fields: model_field_builders(model, relation_placeholders), manifestation: model.database_name().map(|s| s.to_owned()), @@ -142,8 +143,8 @@ pub(crate) fn relation_builders( .map(|r| RelationBuilder { name: r.name(), manifestation: r.manifestation(), - model_a_name: r.model_a.name.clone(), - model_b_name: r.model_b.name.clone(), + model_a_id: r.model_a.id, + model_b_id: r.model_b.id, relation_mode, }) .collect() @@ -204,9 +205,7 @@ pub(crate) fn relation_placeholders(datamodel: &dml::Datamodel) -> Vec Vec { RelationLinkManifestation::Inline(InlineRelation { - in_table_of_model_name: in_table_of_model.to_string(), + in_table_of_model: *in_table_of_model, }) } } diff --git a/query-engine/prisma-models/src/builders/model_builder.rs b/query-engine/prisma-models/src/builders/model_builder.rs index 3f6b185ffdea..a0a6b03a7341 100644 --- a/query-engine/prisma-models/src/builders/model_builder.rs +++ b/query-engine/prisma-models/src/builders/model_builder.rs @@ -1,10 +1,12 @@ use super::{FieldBuilder, IndexBuilder, PrimaryKeyBuilder}; use crate::{CompositeTypeRef, Fields, InternalDataModelWeakRef, Model, ModelRef}; use once_cell::sync::OnceCell; +use psl::schema_ast::ast; use std::sync::Arc; #[derive(Debug)] pub struct ModelBuilder { + pub id: ast::ModelId, pub name: String, pub fields: Vec, pub manifestation: Option, @@ -21,6 +23,7 @@ impl ModelBuilder { composite_types: &[CompositeTypeRef], ) -> ModelRef { let model = Arc::new(Model { + id: self.id, name: self.name, manifestation: self.manifestation, fields: OnceCell::new(), diff --git a/query-engine/prisma-models/src/builders/relation_builder.rs b/query-engine/prisma-models/src/builders/relation_builder.rs index 9bcfdf2dbc83..4da4e62928ef 100644 --- a/query-engine/prisma-models/src/builders/relation_builder.rs +++ b/query-engine/prisma-models/src/builders/relation_builder.rs @@ -1,16 +1,14 @@ -use std::sync::Arc; - -use once_cell::sync::OnceCell; -use psl::datamodel_connector::RelationMode; - use crate::{InternalDataModelWeakRef, Relation, RelationLinkManifestation, RelationRef}; +use once_cell::sync::OnceCell; +use psl::{datamodel_connector::RelationMode, schema_ast::ast}; +use std::sync::Arc; #[derive(Debug)] pub struct RelationBuilder { pub name: String, pub manifestation: RelationLinkManifestation, - pub model_a_name: String, - pub model_b_name: String, + pub model_a_id: ast::ModelId, + pub model_b_id: ast::ModelId, pub relation_mode: RelationMode, } @@ -19,8 +17,8 @@ impl RelationBuilder { let relation = Relation { name: self.name, manifestation: self.manifestation, - model_a_name: self.model_a_name, - model_b_name: self.model_b_name, + model_a_id: self.model_a_id, + model_b_id: self.model_b_id, relation_mode: self.relation_mode, model_a: OnceCell::new(), model_b: OnceCell::new(), diff --git a/query-engine/prisma-models/src/field/relation.rs b/query-engine/prisma-models/src/field/relation.rs index 92afe7af1968..dd9b116f0dca 100644 --- a/query-engine/prisma-models/src/field/relation.rs +++ b/query-engine/prisma-models/src/field/relation.rs @@ -116,7 +116,7 @@ impl RelationField { self.model() .internal_data_model() .find_relation( - (&self.model().name, &self.relation_info.referenced_model), + (self.model().id, self.relation_info.referenced_model), &self.relation_name, ) .unwrap() @@ -141,7 +141,7 @@ impl RelationField { if is_self_rel { !self.relation_info.references.is_empty() } else { - m.in_table_of_model_name == self.model().name + m.in_table_of_model == self.model().id } } _ => false, diff --git a/query-engine/prisma-models/src/internal_data_model.rs b/query-engine/prisma-models/src/internal_data_model.rs index 626c390f1059..5fb95885347a 100644 --- a/query-engine/prisma-models/src/internal_data_model.rs +++ b/query-engine/prisma-models/src/internal_data_model.rs @@ -1,5 +1,6 @@ use crate::{parent_container::ParentContainer, prelude::*, CompositeTypeRef, InternalEnumRef}; use once_cell::sync::OnceCell; +use psl::schema_ast::ast; use std::sync::{Arc, Weak}; pub type InternalDataModelRef = Arc; @@ -52,16 +53,28 @@ impl InternalDataModel { .ok_or_else(|| DomainError::ModelNotFound { name: name.to_string() }) } + pub fn find_model_by_id(&self, model_id: ast::ModelId) -> ModelRef { + self.models + .get() + .and_then(|models| models.iter().find(|model| model.id == model_id)) + .cloned() + .unwrap() + } + /// This method takes the two models at the ends of the relation as a first argument, because /// relation names are scoped by the pair of models in the relation. Relation names are _not_ /// globally unique. - pub fn find_relation(&self, model_names: (&str, &str), relation_name: &str) -> crate::Result { + pub fn find_relation( + &self, + model_ids: (ast::ModelId, ast::ModelId), + relation_name: &str, + ) -> crate::Result { self.relations .get() .and_then(|relations| { relations .iter() - .find(|relation| relation_matches(relation, model_names, relation_name)) + .find(|relation| relation_matches(relation, model_ids, relation_name)) }) .map(Arc::downgrade) .ok_or_else(|| DomainError::RelationNotFound { @@ -85,11 +98,11 @@ impl InternalDataModel { pub fn fields_refering_to_field(&self, field: &ScalarFieldRef) -> Vec { match &field.container { ParentContainer::Model(model) => { - let model_name = &model.upgrade().unwrap().name; + let model_id = model.upgrade().unwrap().id; self.relation_fields() .iter() - .filter(|rf| &rf.relation_info.referenced_model == model_name) + .filter(|rf| rf.relation_info.referenced_model == model_id) .filter(|rf| rf.relation_info.references.contains(&field.name)) .map(Arc::clone) .collect() @@ -124,20 +137,20 @@ impl InternalDataModel { /// /// In other words, the scope for a relation name is only between two models. Every pair of models /// has its own scope for relation names. -fn relation_matches(relation: &Relation, model_names: (&str, &str), relation_name: &str) -> bool { +fn relation_matches(relation: &Relation, model_ids: (ast::ModelId, ast::ModelId), relation_name: &str) -> bool { if relation.name != relation_name { return false; } - if relation.is_self_relation() && model_names.0 == model_names.1 && relation.model_a_name == model_names.0 { + if relation.is_self_relation() && model_ids.0 == model_ids.1 && relation.model_a_id == model_ids.0 { return true; } - if model_names.0 == relation.model_a_name && model_names.1 == relation.model_b_name { + if model_ids.0 == relation.model_a_id && model_ids.1 == relation.model_b_id { return true; } - if model_names.0 == relation.model_b_name && model_names.1 == relation.model_a_name { + if model_ids.0 == relation.model_b_id && model_ids.1 == relation.model_a_id { return true; } diff --git a/query-engine/prisma-models/src/model.rs b/query-engine/prisma-models/src/model.rs index 38f49107b6db..6c8c3c13d0be 100644 --- a/query-engine/prisma-models/src/model.rs +++ b/query-engine/prisma-models/src/model.rs @@ -1,5 +1,6 @@ use crate::prelude::*; use once_cell::sync::OnceCell; +use psl::schema_ast::ast; use std::{ fmt::Debug, hash::{Hash, Hasher}, @@ -10,6 +11,7 @@ pub type ModelRef = Arc; pub type ModelWeakRef = Weak; pub struct Model { + pub id: ast::ModelId, pub(crate) name: String, pub(crate) manifestation: Option, pub(crate) fields: OnceCell, diff --git a/query-engine/prisma-models/src/relation.rs b/query-engine/prisma-models/src/relation.rs index 801f9e49e65e..d8bd57b533c3 100644 --- a/query-engine/prisma-models/src/relation.rs +++ b/query-engine/prisma-models/src/relation.rs @@ -1,7 +1,7 @@ use crate::prelude::*; use dml::ReferentialAction; use once_cell::sync::OnceCell; -use psl::datamodel_connector::RelationMode; +use psl::{datamodel_connector::RelationMode, schema_ast::ast}; use std::{ fmt::Debug, sync::{Arc, Weak}, @@ -15,8 +15,8 @@ pub type RelationWeakRef = Weak; pub struct Relation { pub(crate) name: String, - pub(crate) model_a_name: String, - pub(crate) model_b_name: String, + pub(crate) model_a_id: ast::ModelId, + pub(crate) model_b_id: ast::ModelId, pub(crate) model_a: OnceCell, pub(crate) model_b: OnceCell, @@ -52,14 +52,14 @@ impl Relation { /// A model that relates to itself. For example a `Person` that is a parent /// can relate to people that are children. pub fn is_self_relation(&self) -> bool { - self.model_a_name == self.model_b_name + self.model_a_id == self.model_b_id } /// A pointer to the first `Model` in the `Relation`. pub fn model_a(&self) -> ModelRef { self.model_a .get_or_init(|| { - let model = self.internal_data_model().find_model(&self.model_a_name).unwrap(); + let model = self.internal_data_model().find_model_by_id(self.model_a_id); Arc::downgrade(&model) }) .upgrade() @@ -70,7 +70,7 @@ impl Relation { pub fn model_b(&self) -> ModelRef { self.model_b .get_or_init(|| { - let model = self.internal_data_model().find_model(&self.model_b_name).unwrap(); + let model = self.internal_data_model().find_model_by_id(self.model_b_id); Arc::downgrade(&model) }) .upgrade() @@ -169,8 +169,8 @@ impl Debug for Relation { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Relation") .field("name", &self.name) - .field("model_a_name", &self.model_a_name) - .field("model_b_name", &self.model_b_name) + .field("model_a_name", &self.model_a_id) + .field("model_b_name", &self.model_b_id) .field("model_a", &self.model_a) .field("model_b", &self.model_b) .field("field_a", &self.field_a) @@ -189,7 +189,7 @@ pub enum RelationLinkManifestation { #[derive(Debug, Clone, PartialEq)] pub struct InlineRelation { - pub in_table_of_model_name: String, + pub in_table_of_model: ast::ModelId, } #[derive(Debug, Clone, PartialEq)] diff --git a/query-engine/prisma-models/tests/datamodel_converter_tests.rs b/query-engine/prisma-models/tests/datamodel_converter_tests.rs index 4dbbd1fd35d0..67bc04efc908 100644 --- a/query-engine/prisma-models/tests/datamodel_converter_tests.rs +++ b/query-engine/prisma-models/tests/datamodel_converter_tests.rs @@ -351,7 +351,7 @@ fn explicit_relation_fields() { let relation_name = "BlogToPost"; let blog = datamodel.assert_model("Blog"); let post = datamodel.assert_model("Post"); - let relation = datamodel.assert_relation(("Blog", "Post"), relation_name); + let relation = datamodel.assert_relation((blog.id, post.id), relation_name); blog.assert_relation_field("posts") .assert_list() @@ -369,7 +369,7 @@ fn explicit_relation_fields() { .assert_model_a("Blog") .assert_model_b("Post") .assert_manifestation(&RelationLinkManifestation::Inline(InlineRelation { - in_table_of_model_name: "Post".to_string(), + in_table_of_model: post.id, })); } @@ -392,7 +392,7 @@ fn many_to_many_relations() { let relation_name = "BlogToPost"; let blog = datamodel.assert_model("Blog"); let post = datamodel.assert_model("Post"); - let relation = datamodel.assert_relation(("Blog", "Post"), relation_name); + let relation = datamodel.assert_relation((blog.id, post.id), relation_name); blog.assert_relation_field("posts") .assert_list() @@ -549,7 +549,7 @@ fn convert(datamodel: &str) -> Arc { trait DatamodelAssertions { fn assert_model(&self, name: &str) -> Arc; - fn assert_relation(&self, models: (&str, &str), name: &str) -> Arc; + fn assert_relation(&self, models: (ast::ModelId, ast::ModelId), name: &str) -> Arc; } impl DatamodelAssertions for InternalDataModel { @@ -557,7 +557,7 @@ impl DatamodelAssertions for InternalDataModel { self.find_model(name).unwrap() } - fn assert_relation(&self, models: (&str, &str), name: &str) -> Arc { + fn assert_relation(&self, models: (ast::ModelId, ast::ModelId), name: &str) -> Arc { self.find_relation(models, name).unwrap().upgrade().unwrap() } }