Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Composite Primary Key on Model::updateOrCreate #55585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
totop275 opened this issue Apr 29, 2025 · 1 comment
Closed

Composite Primary Key on Model::updateOrCreate #55585

totop275 opened this issue Apr 29, 2025 · 1 comment

Comments

@totop275
Copy link

Laravel Version

11.44.0

PHP Version

8.3.15

Database Driver & Version

mysql:5.7 (docker image)

Description

When using Laravel’s updateOrCreate with a where condition that includes multiple columns, the update process still only relies on the model’s primaryKey, instead of considering all the where columns provided.

Example:

static::updateOrCreate([
    'website_id' => $activeWebsiteId,
    'key' => $key,
], [
    'value' => $value,
    'encrypted' => $isEncrypted,
]);

In this example:

  • The model has protected $primaryKey = 'key'. However, the actual logical primary key is composite: (key, website_id). Since Eloquent does not natively support composite primary keys, only key is set in the model.
  • The where clause includes both website_id and key.
  • However, during the update operation, Laravel generates a query that only uses key for identifying the record, ignoring website_id.

Impact:

  • The wrong record might get updated, especially if multiple records share the same key but have different website_id values.
  • Data integrity issues occur because the update does not correctly match the full unique constraint expected.

Expected Behavior:

  • updateOrCreate should locate and update records based on all the provided where conditions, not just the primaryKey.
  • The primaryKey setting should not override the behavior of updateOrCreate when multiple matching columns are explicitly specified.

Steps To Reproduce

Doing updateOrCreate on a model for table that has composit primary key or no primary key

@AndrewMast
Copy link
Contributor

AndrewMast commented Apr 29, 2025

Since composite primary keys are not really supported, could you provide an example project to recreate this issue and the type of sql query you would like the updateOrCreate method to run?

Or better yet, make a PR that adds the suggested where conditions to the query and then we can debate if it is backwards compatible.

@laravel laravel locked and limited conversation to collaborators Apr 29, 2025
@crynobone crynobone converted this issue into discussion #55586 Apr 29, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants