phalcon/cphalcon · error · RelationshipNotFound

Cannot find a relationship between

Error message

Cannot find a relationship between '{model}' and '{relationship}' when preparing: {phql}

What it means

Thrown in Query::prepareSelect when processing the 'with' clause: neither getRelationByAlias nor getRelationsBetween finds a relation between the queried model and the requested related model, so eager loading cannot proceed. The input at fault is the relation name in with().

Solutions

  1. Define the relation (hasMany/belongsTo/hasOne) on the model or register its alias via getRelationByAlias.
  2. Check the relation name spelling passed to query->with() or the with clause.
  3. Verify the related model class name matches the one used in the relation definition.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phalcon/Mvc/Model/Query.zep:4069 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/686c52a3076107c7. Report an issue: GitHub.

Appendix: source

Thrown at phalcon/Mvc/Model/Query.zep:4069

                        relationModel = withItem["name"];

                    let relation = manager->getRelationByAlias(
                        modelName,
                        relationModel
                    );

                    if typeof relation == "object" {
                        let bestAlias = relation->getOption("alias"),
                            relationModel = relation->getReferencedModel(),
                            eagerType = relation->getType();
                    } else {
                        let relation = manager->getRelationsBetween(
                            modelName,
                            relationModel
                        );

                        if unlikely typeof relation != "object" {
                            throw new RelationshipNotFound(modelName, relationModel, this->phql);
                        }

                        let bestAlias = relation->getOption("alias"),
                            relationModel = relation->getReferencedModel(),
                            eagerType = relation->getType();
                    }

                    let selectColumns[] = [
                        "type":      PHQL_T_DOMAINALL,
                        "column":    joinAlias,
                        "eager":     alias,
                        "eagerType": eagerType,
                        "balias":    bestAlias
                    ];

                    let automaticJoins[] = [
                        "type": PHQL_T_INNERJOIN,
                        "qualified": [

View on GitHub (pinned to b7419de9cd)