The Great Magnet. What a fool I was to defy him.

Django/MySQL: Operand should contain 1 column(s)

May 14, 2008 · 4 Comments

Update 12/31/2009:

Straight to the point: This MySQL error in combination with Django might be because django’s ORM is expecting one value when it receives two (example: Model.objects.filter(slug=(‘value1′,’value2′))) or anything that causes 2 values to be returned and passed into the ORM.

Look for places directly affecting the ORM (anything that converts your python code to SQL.. so anything that does a Query).

This is an error I recently got in Django and I couldn’t figure out why.

It is errors like this that don’t give an explanation for WHERE it’s being called that get me on a wild goose chase.

This is a MySQL error meaning you need to use AND. But since Django’s ORM takes care of the SQL for us, it’s hard for us casual users to know whats up.

Turns out one of my statements was passing a pair of values (value1, value2) to the object manager (filter).

So: If you have something like: some_model.objects.filter(field__lte=something), make sure that ’something’ is a single value, not a pair.

Now it makes sense why a two value pair would throw that MySQL error, if Django treats it like a single value.

Update: Comment from Ben might help others with this problem:

For others who may hit this, I suspect your problem is that get_or_create actually returns a tuple! (the value you think it should return, boolean telling you if the value was created)

Categories: Django
Tagged: ,

4 responses so far ↓

Leave a Comment