FileField paths causes "duplicate entry" error when saving a node with empty fields

Earl Grey - November 7, 2009 - 17:14
Project:FileField Paths
Version:6.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

Hey folks,

when saving a newly created node with some fields empty, cck tries to save these empty values into the database, but an empty value already exists. Here is the error message:

user warning: Duplicate entry '287-0' for key 1 query: INSERT INTO intern_content_field_wr_files (vid, nid, delta, field_wr_files_fid, field_wr_files_list, field_wr_files_data) VALUES (287, 166, 0, NULL, NULL, NULL) in /htdocs/sites/all/modules/cck/content.module on line 1213.

I get three of these messages, as there are three fields left blank.

The node is saved properly and I can edit and save it again withour errors.

The field types (in this case) are: file, link and node reference. There is no error message for a fourth field, that I left blank. The field type of that field is text.

Any ideas on that?

#1

nikitas - November 7, 2009 - 18:03

have you checked how are those fields are saved into database?could it be because a unique feature is activated for either the cck field or is something from the administration panel that is checked?

#2

Earl Grey - November 9, 2009 - 10:01

I couldn't find a 'unique' feature for cck fields. In my opinion, empty fields should not be stored in DB at all!

I have some more information about that issue:

- it only appears, if I set the number of values to a number higher than '1' (includes unlimited)
- if I use a fixed number of items, the error occurs in that same ammount (2 items - 2 warnings)
- with one fixed item, there is no erro
- with an 'unlimited' number of items, there is ony one warning.

I can't remember that warning prior to updating to cck 2.6, but have no idea what change could cause this.

The table intern_content_field_wr_files (for example) contains 79 rows, most of them with NULL values. Is that the way it should be?

I took a closer look at the DB: the values, that should be stored (287-0 in the example) do exist in the DB, so I guess, cck tries to store them twice, for some reason. The shouldn't be stored if they have no value (in my opinion).

Can I trace the call of php functions in cck somehow?

#3

markus_petrux - November 10, 2009 - 00:22
Status:active» postponed (maintainer needs more info)

When updating a node, CCK deletes all records of the vid in the tables of multiple value fields, then proceeds to insert the new records. When inserting, it uses a loop where delta is the key, so it is not possible to create dups while inserting, and it shouldn't be possible either, if the previous records where removed. See the code:

<?php
     
// Handle multiple fields.
     
foreach ($type['fields'] as $field) {
        if (
$field['multiple'] && isset($node->$field['field_name'])) {
         
$db_info = content_database_info($field);
         
// Delete and insert, rather than update, in case a value was added.
         
if ($op == 'update') {
           
db_query('DELETE FROM {'. $db_info['table'] .'} WHERE vid = %d', $node->vid);
          }
          foreach (
$node->$field['field_name'] as $delta => $item) {
           
$record = array();
            foreach (
$db_info['columns'] as $column => $attributes) {
             
$record[$attributes['column']] = $item[$column];
            }
           
$record['nid'] = $node->nid;
           
$record['vid'] = $node->vid;
           
$record['delta'] = $delta;
           
content_write_record($db_info['table'], $record);
          }
        }
      }
?>

So, it seems there's something odd in your system.

Note that CCK deletes when $op is 'update'. This comes from node_save(), and it is set if nid is not empty in node object. So, if CCK does not perform the deletes, it could be that node_save() triggers an insert operation, and that could happen where it shouldn't if the nid is empty on a node object passed to node_save().

#4

Earl Grey - November 10, 2009 - 11:36

This happens only when creating a new node, so $op should never be 'update' when saving a new node. I guess, that the code you posted (in which file is that code?) is executed twice with $op != 'update'.

I figured out that this error only occurs, when FileField Paths is activated, which is using the token module. Is it possible, that the node needs to be saved, before tokens can be evaluated and the node is saved a second time, after the filefield path has been generated with the use of tokens?

Which values are possible for $op? Would it be possible to delete all lines in the DB, even if the node is saved the first time? I think, this is a performance issue, but that query should be executed very quickly, if there is no line to delete in the table.

I'll have a look at the module(s) causing that issue. Maybe I find something interesting in the code.

#5

Earl Grey - November 10, 2009 - 11:55
Title:Receiving "duplicate entry" error when saving a node with empty fields» FileField paths causes "duplicate entry" error when saving a node with empty fields
Project:Content Construction Kit (CCK)» FileField Paths
Version:6.x-2.6» 6.x-1.x-dev
Component:content.module» Code
Status:postponed (maintainer needs more info)» active

Moving this to FileField Paths.

 
 

Drupal is a registered trademark of Dries Buytaert.