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

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)

7 thoughts on “Django/MySQL: Operand should contain 1 column(s)

  1. Hah! I just ran into this problem again, and remembered I wrote something like it. Sure enough, I was passing a pair value instead of a string.

  2. Hey thanks for posting this — just saved me a lot of time 🙂

    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)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s