Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Given this table structure:

CREATE TABLE tags
(
  id SERIAL NOT NULL PRIMARY KEY,
  tagname TEXT NOT NULL UNIQUE,
  authorid int NOT NULL,
  created timestamp NOT NULL,
  lastmodified timestamp NOT NULL,

  constraint fk_authorid_tags foreign key(authorid) references users(id)
);

Why does the following query fails with the error:

ERROR:  operator does not exist: text = text[]
LINE 2: select * from tags where tagname in ('{"c#","c"}'::text[])

Query:

select * from tags where tagname in ('{"c#","c"}'::text[])
share|improve this question

1 Answer 1

up vote 5 down vote accepted

IN must contain a literal list, e.g.

tagname IN ('c#', 'c')

If you want an array, you must use = ANY:

tagname = ANY (ARRAY['c#', 'c'])

The error is arising because tagname IN (somearray) is interpreted as the query "is tagname equal to any element of the the 1-element list (somearray)". That means testing tagname for equality against somearray, the only element. As there's no = operator to compare text and text[], this fails.

By contrast, = ANY says "For any element of the array on the right hand side, is the left hand operand equal to the element?". So it works.

share|improve this answer
    
Thanks for the thorough explanation Craig. –  Oliver Weichhold Aug 20 '14 at 8:40
    
well explained. –  unique_id Aug 20 '14 at 9:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.