We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Value of field does not exist on referenced table?

I'm trying to update a field eb_health_coach_id in a table called eb_health_coach but I'm getting the below error when I try to set it as $evaluationEntry->setEbHealthCoachId($coachId);:

Value of field eb_health_coach_id does not exist on referenced table?

*Action for updating the FK field: **

  public function saveHealthCoachAction()
  {
      // Get POST Parameters
      $id = $this->request->getPost('id', 'int');

      $coachId = $this->request->get( 'coach_id', null );

      $evaluationEntry = null;

      if( $id ) {
          // Get Evaluation Entry from DB
          $evaluationEntry = EbEvaluationClient::findFirst($id);
      }

      // If any parameter is missing or entry is not found, return error
      if(!$evaluationEntry) {
          $this->setJsonResponse(false);
          return array('error' => 'Invalid data');
      }

      // Set Health Coach
      $evaluationEntry->setEbHealthCoachId($coachId); // Error occurs here while updating eb_health_coach_id
      $evaluationEntry->update();

      // Return success
      return array('data' => 'Success');
  }

EbEvaluationClient Model:

    public function setEbHealthCoachId($id)
    {
        $this->eb_health_coach_id = $id;
        return $this;
    }

    public function initialize() {
        // Run base initialize code
        parent::initialize();

        // Add relation for the Health Coach
        $this->belongsTo('eb_health_coach_id', EbHealthCoach::class,'id', array(
            'alias' => 'EbHealthCoach',
            'foreignKey' => array('allowNulls' => true)
        ));
        // Add relation for the Result (where JSON result is stored)
        $this->hasOne('id', EbEvaluationClientResult::class,'eb_evaluation_client_id', array(
            'alias' => 'EbEvaluationClientResult',
            'cxAction' => static::ACTION_CASCADE_DELETE
        ));
    }

EbHealthCoach Model:

  public function initialize() {
      // Run base initialize code
      parent::initialize();

      // Add relation for the CX User
      $this->belongsTo('cx_user_id', CxUserExtension::class,'id', array(
          'alias' => 'CxUserExtension',
          'foreignKey' => true
      ));
  }

Schema for tables:

  CREATE TABLE `eb_evaluation_client` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `eb_health_coach_id` bigint(20) unsigned DEFAULT NULL,
    `first_name` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
    `last_name` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL,
    `date_created` int(11) unsigned NOT NULL,
    `date_updated` int(11) unsigned DEFAULT NULL,
    `external_ref` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `fk_eb_evaluation_client_eb_health_coach_id_idx` (`eb_health_coach_id`),
    CONSTRAINT `fk_eb_evaluation_client_eb_health_coach_id` FOREIGN KEY (`eb_health_coach_id`) REFERENCES `eb_health_coach` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
  ) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

  CREATE TABLE `eb_health_coach` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `cx_user_id` bigint(20) unsigned NOT NULL,
    `date_created` int(11) unsigned NOT NULL,
    `date_updated` int(11) unsigned DEFAULT NULL,
    `external_ref` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `fk_eb_health_coach_cx_user_id_idx` (`cx_user_id`),
    CONSTRAINT `fk_eb_health_coach_cx_user_id` FOREIGN KEY (`cx_user_id`) REFERENCES `cx_user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
  ) DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


8.4k

if you recently updated the table schema you should reset the meta data cache if thats not the case paste full EbHealthCoach Model