Jump to content

Duplicate service field constraint violation in Services->setFields() method


Recommended Posts

Posted

Problem: Pterodactyl game eggs occasionally have the below problem
The setFields() method in /app/models/services.php doesn't handle duplicate field keys in the input array. When modules or package configurations pass duplicate service fields (same key), the method attempts to insert multiple records with the same service_id + key combination, causing a primary key constraint violation:

Quote

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'XX-server_password' for key 'PRIMARY'

Root Cause:
In line ~4165, the method loops through $vars and calls addField() for each entry without checking for duplicate keys. If $vars contains multiple entries with the same key, multiple INSERT statements are executed for the same service_id + key, violating the primary key constraint.

Solution:
Add deduplication logic before processing the fields array. Here's the diff that fixed the problem and stopped white redirect pages from occurring:
 

Quote

--- services.php.orig
+++ services.php
@@ -4161,6 +4161,14 @@
         }
 
         if (!empty($vars)) {
+            // Deduplicate fields by key to prevent duplicate key constraint violations
+            $unique_fields = [];
+            foreach ($vars as $field) {
+                if (isset($field['key'])) {
+                    $unique_fields[$field['key']] = $field;
+                }
+            }
+            $vars = array_values($unique_fields);
             foreach ($vars as $field) {
                 $this->addField($service_id, $field);
             }

Benefits of this fix:
1. Prevents crashes: Eliminates primary key constraint violations
2. Maintains functionality: If duplicates exist, keeps the last occurrence (allows intentional overriding)
3. Backward compatible: No API changes, same input/output behavior
4. Future-proof: Protects against any module/package that might pass duplicate fields

Affected scenarios:
•  Custom modules that generate duplicate service fields
•  Package configurations with conflicting field definitions  
•  Third-party integrations (like Pterodactyl) with complex field mappings

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...