0
from django.contrib.auth.models import User as DjangoUser
class Ward(models.Model):
    user = models.ForeignKey(DjangoUser, related_name='wards')
    group = models.ForeignKey(Group, related_name='wards')

This is my django model and I use this filter.

Group.objects.filter(wards__user=_user).all()

I used this code in sqlite3, it works well.

But, it doesn't work in PostgreSQL.

operator does not exist: character varying = integer LINE 1: ...rchive_ward"."group_id" ) WHERE "archive_ward"."user_id" = 1

I think it is caused by user_id field in archive_ward tables. I found this field's data type is character.varying(20).

What can I do for this code?

4
  • Do you need to make migrations? have you changed the type of field for user? Commented Dec 4, 2015 at 7:54
  • No, I didn't change django user. But I have another User Model in same models.py. So, I use from django.contrib.auth.models import User as DjangoUser. Commented Dec 4, 2015 at 7:59
  • @Sayse, I got data from other site like post, group, user, and etc. Commented Dec 4, 2015 at 8:04
  • Ignore my last comment, confused myself! Commented Dec 4, 2015 at 8:05

2 Answers 2

4

Try removing the user table in the database and adding it again.

create a new one from scratch. Syncing database again will work..

or else You can do like this Way raw_query

You cannot compare an integer with a varchar. PostgreSQL is strict and does not do any magic typecasting for you. I'm guessing SQLServer does typecasting automagically (which is a bad thing).

If you want to compare these two different beasts, you will have to cast one to the other using the casting syntax ::

Sign up to request clarification or add additional context in comments.

Comments

0

The Postgres error means you're comparing an integer to a string:

operator does not exist: character varying = integer

You could change the database model so user_id is of an integer type. Or you could cast the integer to string in Python:

Group.objects.filter(wards__user=str(_user)).all()

1 Comment

unfortunately, it doesn't work. Because str(_user) return username. So I do Group.objects.filter(wards__user_id=str(_user.id)).all(), but it has same operator does not exist: character varying = integer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.