cphalcon
cphalcon copied to clipboard
[BUG]: Model->save() triggers Insert instead of Update when records is existing one
We're encountering intermittent issues with a specific Model (the only one affected). Occasionally, when attempting to execute ->save() or ->delete(), we encounter database errors such as:
"Unique violation: 7 ERROR: duplicate key value violates unique constraint."
This issue persists from version 3.4 up to 5.8.0 and only occurs on the production server. We haven't been able to reproduce it locally. The only temporary solution on the production server is clearing the Redis cache.
Previously, we encountered another error:
"A primary key must be defined in the model in order to perform the operation."
We resolved this by adding the following annotation to the model's $id:
/**
* @Primary
* @Identity
* @Column(type='integer', nullable=false)
*/
protected $id;
We suspect that the problem might be related to the metadata cache for this model. After reviewing the Zephir code, it seems the issue occurs at:
Model.zep::has(<MetaDataInterface> metaData, <AdapterInterface> connection), where the record is not recognized as existing in the database.
Since we can't reproduce the issue locally, I'm unable to provide code or specific examples. We've also tried clearing the Redis cache (ph-mm-reds-meta-[some namespace lowercase]\standingorderpattern and ph-mm-reds-map-[some namespace lowercase]\standingorderpattern) before querying the database, but the problem persists.
Details
- Phalcon version: 5.8.0
- PHP Version: 8.2.20
- Operating System: Linux
- Installation type: installing via package manager
- Zephir version (if any):
- Server: Apache
- Other related info (Database, table schema): postgres db, redis for cache