Home > front end >  Django Model with Oracle Database returns Error: ORA-00904 invalid identifier
Django Model with Oracle Database returns Error: ORA-00904 invalid identifier

Time:09-12

i try to connect two database tables with a foreign key and i get the error
ORA-00904: "A"."A_NR_ID": invalid identifier
for the model:

class A(models.Model):
    id = models.IntegerField(primary_key=True)
    a_nr = models.ForeignKey(B, models.DO_NOTHING)
--> #anr = models.ForeignKey(B, models.DO_NOTHING, db_column="a_nr")
    f_b = models.CharField(max_length=1)
    ...

class B(models.Model):
    id = models.IntegerField(primary_key=True)
    ...

If i replace a_nr with the comment-line anr it works. And i have no idea why, since the name only uses single underscore. Also the column f_b seems to work perfectly fine.
If i then run "makemigrations" it trys to delete the column a_nr and create anr, which also makes no sense to me, since i thought db_column="a_nr" would keep the actuall name in the oracle database the same.

The second error is:
ORA-00904: "D"."BEZEICHNUNG_ID": invalid identifier
for the model:

class C(models.Model):
    id = models.IntegerField(primary_key=True)
    bezeichnung = models.CharField()


class D(models.Model):
    id = models.IntegerField(primary_key=True)
    bezeichnung =  models.ForeignKey(C, models.DO_NOTHING)
    ...

And here i can't figure out what's wrong.

CodePudding user response:

If I replace a_nr with the comment-line anr it works. And I have no idea why, since the name only uses single underscore.

This has nothing to do with the name of the field, but that of the database: for a ForeignKey, and OneToOneField, if your field is named fieldname, it will use as database column by default fieldname_id. You can use the db_column=… parameter [Django-doc] to specify the name of the column to use, so:

class A(models.Model):
    a_nr = models.ForeignKey(B, models.DO_NOTHING, db_column='a_nr')
    # …


class D(models.Model):
    bezeichnung = models.ForeignKey(C, models.DO_NOTHING, db_column='bezeichnung')
    # …
  • Related