I have two taxonomy vocabularies selected as unique fields, uniqueness is set for each individually, not in combination.

If a node is saved with a term selected in only one of the two vocabularies (i.e., no term is selected in the other vocabulary), the user gets this warning message:

user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND node.type = 'mynodetype'' at line 1 query: SELECT node.nid FROM node node JOIN term_node term_node USING (vid) WHERE term_node.tid IN() AND node.type = 'mynodetype' in /var/www/drupal/sites/all/modules/unique_field/unique_field.module on line 381.

Despite this warning, everything appears to work properly. The node is saved, and no additional nodes can be created with the same term. But I thought I'd bring this to your attention.

In case it matters: I'm using CCK3, also Vocabulary Permissions.

Comments

anshuman’s picture

The reason this is happening is cause the SQL, if there were terms, would be as follows:

SELECT node.nid FROM node node JOIN term_node term_node USING (vid) WHERE term_node.tid IN(MY_TAXONOMY_TERMS) AND node.type = 'mynodetype'

But when MY_TAXONOMY_TERMS is empty, the SQL becomes

SELECT node.nid FROM node node JOIN term_node term_node USING (vid) WHERE term_node.tid IN() AND node.type = 'mynodetype'

and "IN()" leads to the SQL failure as this is not acceptable. It should be IN('') or IN("").

The reason things seem to work is because although the query failed to execute, there was no checks to be done anyway as the term was not there anyway (which lead to the empty IN clause).

Cheers,
Anshuman

anshuman’s picture

In unique_field.module,

// generate query where clause for taxonomy terms
elseif (strpos($field, UNIQUE_FIELD_FIELDS_TAXONOMY) === 0) {
$qtbl = 'term_node';

$qwhere = "term_node.tid IN(". join(',', $values) .") ";

The case for when $values is null has to be handled. One option is to do something as follows:

// generate query where clause for taxonomy terms
elseif (strpos($field, UNIQUE_FIELD_FIELDS_TAXONOMY) === 0) {
$qtbl = 'term_node';

if (empty($values))
{
$values = array('""');
}

$qwhere = "term_node.tid IN(". join(',', $values) .") ";

Sinovchi’s picture

Status: Active » Needs review

I had this error when I use Unique field with Automatic Nodetitles. Changing code as #2 is working for me. Thanks for solution.