Allee Clark
Allee Clark

Reputation: 149

Django shell ORA-00904

I have a legacy database from oracle that has fields. If I run Orders.objects.count() I get the correct amount. When I run Orders.object.all() I get this error but haven't found a solution online.

class Orders(models.Model):
account_obj_db = models.BigIntegerField(blank=True, null=True)
account_obj_id0 = models.BigIntegerField(blank=True, null=True)
account_obj_type = models.CharField(max_length=1020, blank=True, null=True)
account_obj_rev = models.BigIntegerField(blank=True, null=True)
order_payload_buf_size = models.BigIntegerField(blank=True, null=True)
total_entitlement_count = models.BigIntegerField(blank=True, null=True)
currency_code = models.BigIntegerField(blank=True, null=True)

>>> from home.models import Orders
>>> Orders.objects.all()
Traceback (most recent call last):
File "/home/user/dev2/lib/python3.5/site- packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/user/dev2/lib/python3.5/site- packages/django/db/backends/oracle/base.py", line 481, in execute
return self.cursor.execute(query, self._param_generator(params))
cx_Oracle.DatabaseError: ORA-00904: "ORDERS"."ID": invalid identifier

Upvotes: 2

Views: 1973

Answers (1)

Mojimi
Mojimi

Reputation: 3161

The issue here is that, when you don't assign a primary key to your models, Django will create a "ID" PK field that is not shown on models.

But when you set managed = false(I assume you did) in your model's meta, Django will not create/manage this table in the database, meaning it will not create the underlying ID pk field, but the model level still thinks it exists (you could consider this a bug and report it) and tries to make queries with it, hence the ORA error

The solution is to manually set the same PK field in your models as you have in your database, let's say account_obj_db is the PK field of Orders in your legacy Database, just add primary_key to it in your models.py :

class Orders(models.Model):
    account_obj_db = models.BigIntegerField(primary_key = true, blank=True, null=True)
    account_obj_id0 = models.BigIntegerField(blank=True, null=True)
    account_obj_type = models.CharField(max_length=1020, blank=True, null=True)
    account_obj_rev = models.BigIntegerField(blank=True, null=True)
    order_payload_buf_size = models.BigIntegerField(blank=True, null=True)
    total_entitlement_count = models.BigIntegerField(blank=True, null=True)
    currency_code = models.BigIntegerField(blank=True, null=True) 

You might need to do another migration after this

Also watch your indenting

Upvotes: 2

Related Questions