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

Entries tagged as ‘MySQL’

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

May 14, 2008 · 4 Comments

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: ,

Duplicate Key IntegrityError Django

May 13, 2008 · Leave a Comment

If you are wondering what part of your Django model is wrong because it is spitting out a nameless IntegrityError, it might be this:

I had a model with Unique=True & Blank=True that caused description-less IntegrityErrors because with two objects that have a field ” (blank), or Null, it was no longer unique.

I wanted the field to be Unique, IF and only if it was not blank or null.

I’ll just use Blank=True, null=True and let it be not unique if it ever does.

Categories: Django
Tagged: ,