CoolLife
CoolLife

Reputation: 1479

Record not found despite being present

well I'm post data json to my backend and I have this error:

{
  "message": "Record not found in table \"users\" with primary key ['6']",
  "url": "/users/userdata",
  "code": 404
}

I don't know why? I have a user with id = 6 in my database, in my function I recieve the data and id correctly, check this part, my controller:

  public function userdata()
    {
        $user = TableRegistry::get('Users');
        $id = $this->request->getData('iduser');

        $data = $user->get($id, [
            //'contain' => ['Cities']
        ]);

        $this->set([
            'success' => true,
            'data' => $data,
            '_serialize' => ['success', 'data']
        ]);
    }

posting data

Try this with $this->Users->get($id) and $this->Users->find($id) I always get the same error

Upvotes: 1

Views: 1295

Answers (1)

ndm
ndm

Reputation: 60463

The error message can be a little bit misleading, as one could think that it's really about trying to access something in the database, however it's actually a InvalidPrimaryKeyException exception (whenever receiving errors, check the logs!).

This happens when the passed primary key format doesn't match the primary key configuration of the table, like for example when the primary key is a composite key, and you're not passing all required columns. It could also happen in case the tables primary key is wrongly configured, for example when it's empty.

Make sure that you're passing all required columns, check your tables setPrimaryKey()/primaryKey() call, make sure to clear the mode cache (tmp/cache/models) to avoid wrong/old schemas to be used, and ensure that the primary key constraint in the database is configured properly.

Upvotes: 1

Related Questions