eveseat / seat

🌀✳️ SeAT: A Simple, EVE Online API Tool and Corporation Manager
https://eveseat.github.io/docs/
GNU General Public License v2.0
425 stars 143 forks source link

Duplicate corporation asset entry #860

Closed warlof closed 2 years ago

warlof commented 2 years ago
./laravel-2022-01-13.log:26556:[2022-01-13 10:43:21] local.ERROR: SQLSTATE[23000]: Integrity constraint violation: 1062
Duplicate entry '1036390695338' for key 'PRIMARY' (
SQL: insert into `corporation_assets` (`corporation_id`, `item_id`, `type_id`, `quantity`, `location_id`, `location_type`, `location_flag`, `is_singleton`, `updated_at`, `created_at`) values (***REDACTED***)
{"exception":"[object] (Illuminate\\Database\\QueryException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1036390695338' for key 'PRIMARY' (
SQL: insert into `corporation_assets` (`corporation_id`, `item_id`, `type_id`, `quantity`, `location_id`, `location_type`, `location_flag`, `is_singleton`, `updated_at`, `created_at`) values (***REDACTED***)
) at /var/www/seat/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669)
recursivetree commented 2 years ago

the id is a unique key, but in the place where we query for an existing itme, there is an additional check for the corporation

ghost commented 2 years ago

This is especially weird because I believe this code is preceded with a call to wipe all of the given corporations assets, so duplicate keys shouldn't be possible.

This occurred likely as a result of the ESI failure early 2022, and was ultimately resolved by manually wiping the effected table, though a fix would be wise as I do not have an easy way to know if other corporation/character IDs have similarly corrupt data.

Issue was still extant on the following versions (but has since been manually worked around so I cannot re-test): PHP Version: 7.4.27 SeAT API Installed: v4.7.0 SeAT Console Installed: v4.8.0 SeAT Eve API Installed: v4.16.0 SeAT Notifications Installed: v4.3.1 SeAT Services Installed: v4.2.0 SeAT Web Installed: v4.17.0 OS: Docker on CentOS 7 host

ghost commented 2 years ago

The file in question. Ignore my earlier comments about corporation assets wiping, that was my own misunderstanding.

https://github.com/eveseat/eveapi/blob/master/src/Jobs/Assets/Corporation/Assets.php#L112

There may be a solution in replacing firstOrNew with updateOrCreate as appropriate.

$model = CorporationAsset::updateOrCreate([
  'corporation_id' => $this->getCorporationId(),
  'item_id' => $asset->item_id,
], [
   // Map in new changed values here
]);

Or potentially adding protected static $guarded = ['item_id']; to the file below, and removing protected static $unguarded = true;

https://github.com/eveseat/eveapi/blob/master/src/Models/Assets/CorporationAsset.php#L148

warlof commented 2 years ago

To me, the issue is much tied to the table primary key (set to item_id) which is distinct from the unicity check (corporation_id + item_id)

I figure the error occurred on an item which has been transferred across corporation.

recursivetree commented 2 years ago

To me, the issue is much tied to the table primary key (set to item_id) which is distinct from the unicity check (corporation_id + item_id)

I figure the error occurred on an item which has been transferred across corporation.

pretty sure that is it. Is there even any reason for the corporation check?

warlof commented 2 years ago

check should match table primary key so, actually, nope :)

ghost commented 2 years ago

To me, the issue is much tied to the table primary key (set to item_id) which is distinct from the unicity check (corporation_id + item_id)

I figure the error occurred on an item which has been transferred across corporation.

This is very possible. It occurred on one of my holding corps, and I have multiple corporations that are tracked and regularly exchange items between them.