0

I'm having a trouble on getting this error when filtering data on my postgresql, it says that the error comes from this line .filter(bene_id='31452'). I've been using two tables in Django. I think the problem is on my operator = since the id is integer but the problem is how and what should I replace in that operator ?

I tried putting double qoute, single qoute and even without elements like this .filter(bene_id="31452") ,.filter(bene_id=31452) ,.filter(bene_id='31452') . But it seems the error has not been solve.it would be great if anybody could figure out where I am doing something wrong. thank you so much in advance

Error

LINE 1: ...2_benes_status" WHERE "b2_benes_status"."bene_id" = 31452  L...
                                                             ^
HINT:  No operator matches the given name and argument types. You might need 
to add explicit type casts.

views.py

Testing = B2BenesStatus.objects.using('matchy_data').filter(bene_id='31452')

Models.py

class B2BenesStatus(models.Model):
    datetime = models.DateTimeField(blank=True, null=True)
    value = models.BooleanField(blank=True, null=True)
    remarks = models.TextField(blank=True, null=True)
    bene_id = models.IntegerField(blank=True, null=True)
    stat_cat_id = models.IntegerField(blank=True, null=True)
    updated_by_id = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'b2_benes_status'

Settings.py

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'payroll',
    'USER':'root',
    'PASSWORD':'',
    'HOST':'localhost',
    'PORT':'3306',

},
    'matchy_data': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'matchy_data',
    'USER':'postgres',
    'PASSWORD':'samplee',
    'HOST':'localhost',
    'PORT':'5432',
},}
Vassily
  • 5,263
  • 4
  • 33
  • 63
Marie Loise
  • 107
  • 1
  • 2
  • 12
  • Change your `id` in your lookup to an integer. https://stackoverflow.com/questions/3739808/no-operator-matches-the-given-name-and-argument-types-you-might-need-to-add-e – PacketLoss Feb 10 '21 at 05:52
  • @PacketLoss but in my models it's already set as integerFiled `bene_id = models.IntegerField(blank=True, null=True)` , Did you mean It should be change this into TextField? – Marie Loise Feb 10 '21 at 05:57
  • 1
    I think the problem is on my operator `=` since the id is integer but the problem is how and what should I replace in that operator ? – Marie Loise Feb 10 '21 at 06:22
  • ORM query is correct. Don't use qoutes because it's integer field. I suggest try again code without qoutes. I have not used second second database and postgresql, so if nothing work (and you have time), you can try a test model in default database with same fields and try this Testing = B2BenesStatus.objects.filter(bene_id=31452) – sandeep Feb 10 '21 at 06:33

2 Answers2

0

Assuming bene_id is really stored as an integer in your existing SQL database (please, double check that):

As bene_id is an IntegerField, .filter(bene_id='31452') will fail, because '31452' is a string type. Also, note there is no fundamental difference between " and ' in Python.

You need pass an int type to the bene_id argument, something like .filter(bene_id=31452).

You said you tried without string delimiters without success, but I suspect there is another problem behind then.

Le Minaw
  • 835
  • 7
  • 17
  • I just found out that the error is when I want to print that query `Testing `. But it should suppose to be print? how should I display `Testing` value ? – Marie Loise Feb 10 '21 at 07:24
  • As QuerySets are lazy, they are not evaluated instantly but when some following code actually uses them. `print(Testing)` should return the `__str__` representation of a `B2BenesStatus` object. – Le Minaw Feb 10 '21 at 08:55
0

Testing = B2BenesStatus.objects.using('matchy_data').filter(bene_id__in=['31452'])

-Now you will not get Error

100 % error free code if you have any doubts please ask here I'm ready to help you.. enter image description here