fn add_missing_back_relations(
        &self,
        ast_schema: &ast::SchemaAst,
        schema: &mut dml::Datamodel,
    ) -> Result<(), Diagnostics> {
        let mut errors = Diagnostics::new();

        let mut missing_back_relation_fields = Vec::new();
        for model in schema.models() {
            let mut missing_for_model = self.find_missing_back_relation_fields(&model, schema, ast_schema)?;
            missing_back_relation_fields.append(&mut missing_for_model);
        }

        for missing_back_relation_field in missing_back_relation_fields {
            let model = schema
                .find_model(&missing_back_relation_field.model)
                .expect(STATE_ERROR);
            let field_name = &missing_back_relation_field.field.name;

            if model.find_relation_field(&field_name).is_some() {
                let source_model = schema
                    .find_model(&missing_back_relation_field.related_model)
                    .expect(STATE_ERROR);
                let source_field = source_model
                    .find_relation_field(&missing_back_relation_field.related_field)
                    .expect(STATE_ERROR);
                errors.push_error(field_validation_error(
                                "Automatic related field generation would cause a naming conflict. Please add an explicit opposite relation field.",
                                &source_model,
                                &Field::RelationField(source_field.clone()),
                                &ast_schema,
                            ));
            } else {
                let model_mut = schema.find_model_mut(&missing_back_relation_field.model);

                model_mut.add_field(Field::RelationField(missing_back_relation_field.field));

                for underlying_field in missing_back_relation_field.underlying_fields.into_iter() {
                    if !model_mut.has_field(&underlying_field.name) {
                        model_mut.add_field(Field::ScalarField(underlying_field));
                    }
                }
            }
        }

        if errors.has_errors() {
            Err(errors)
        } else {
            Ok(())
        }
    }
/// Identifies and adds missing back relations. For 1:1 and 1:N relations.
/// Explicit n:m relations are not touched, as they already have a back relation field.
/// Underlying scalar fields are not added here.
Identifies and adds missing back relations.
Identifies and adds missing back relations .
fn add_missing_back_relations(
        &self,
        ast_schema: &ast::SchemaAst,
        schema: &mut dml::Datamodel,
    ) -> Result<(), Diagnostics> {
        let mut errors = Diagnostics::new();
        let mut missing_back_relation_fields = Vec::new();
        for model in schema.models() {
            let mut missing_for_model = self.find_missing_back_relation_fields(&model, schema, ast_schema)?;
            missing_back_relation_fields.append(&mut missing_for_model);
        }
        for missing_back_relation_field in missing_back_relation_fields {
            let model = schema
                .find_model(&missing_back_relation_field.model)
                .expect(STATE_ERROR);
            let field_name = &missing_back_relation_field.field.name;
            if model.find_relation_field(&field_name).is_some() {
                let source_model = schema
                    .find_model(&missing_back_relation_field.related_model)
                    .expect(STATE_ERROR);
                let source_field = source_model
                    .find_relation_field(&missing_back_relation_field.related_field)
                    .expect(STATE_ERROR);
                errors.push_error(field_validation_error(
                                "Automatic related field generation would cause a naming conflict. Please add an explicit opposite relation field.",
                                &source_model,
                                &Field::RelationField(source_field.clone()),
                                &ast_schema,
                            ));
            } else {
                let model_mut = schema.find_model_mut(&missing_back_relation_field.model);
                model_mut.add_field(Field::RelationField(missing_back_relation_field.field));
                for underlying_field in missing_back_relation_field.underlying_fields.into_iter() {
                    if !model_mut.has_field(&underlying_field.name) {
                        model_mut.add_field(Field::ScalarField(underlying_field));
                    }
                }
            }
        }
        if errors.has_errors() {
            Err(errors)
        } else {
            Ok(())
        }
    }
fn add_missing_back_relations ( & self , ast_schema : & ast :: SchemaAst , schema : & mut dml :: Datamodel , ) -> Result < ( ) , Diagnostics > { let mut errors = Diagnostics :: new ( ) ; let mut missing_back_relation_fields = Vec :: new ( ) ; for model in schema . models ( ) { let mut missing_for_model = self . find_missing_back_relation_fields ( & model , schema , ast_schema ) ? ; missing_back_relation_fields . append ( & mut missing_for_model ) ; } for missing_back_relation_field in missing_back_relation_fields { let model = schema . find_model ( & missing_back_relation_field . model ) . expect ( STATE_ERROR ) ; let field_name = & missing_back_relation_field . field . name ; if model . find_relation_field ( & field_name ) . is_some ( ) { let source_model = schema . find_model ( & missing_back_relation_field . related_model ) . expect ( STATE_ERROR ) ; let source_field = source_model . find_relation_field ( & missing_back_relation_field . related_field ) . expect ( STATE_ERROR ) ; errors . push_error ( field_validation_error ( " " , & source_model , & Field :: RelationField ( source_field . clone ( ) ) , & ast_schema , ) ) ; } else { let model_mut = schema . find_model_mut ( & missing_back_relation_field . model ) ; model_mut . add_field ( Field :: RelationField ( missing_back_relation_field . field ) ) ; for underlying_field in missing_back_relation_field . underlying_fields . into_iter ( ) { if ! model_mut . has_field ( & underlying_field . name ) { model_mut . add_field ( Field :: ScalarField ( underlying_field ) ) ; } } } } if errors . has_errors ( ) { Err ( errors ) } else { Ok ( ( ) ) } }
Identifies and adds missing back relations.
Identifies and adds missing back relations .
&self
ast_schema
&ast::SchemaAst
schema
&mut dml::Datamodel
&self
ast_schema
&ast::SchemaAst
schema
&mut dml::Datamodel