Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django Model with Oracle Database returns Error: ORA-00904 invalid identifier

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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')
    # …
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading