I added New Column FK_Project
in the rw_item
Table, which is reference of the Project_Master
table.
dp=# ALTER TABLE digital_publisher_rw_item ADD FK_Project int;
ALTER TABLE
dp=# ALTER TABLE digital_publisher_rw_item ADD FOREIGN KEY(FK_Project) REFERENCES digital_publisher_project_master(id);
ALTER TABLE
Now I am writing script to add values of to Column FK_Project
in the rw_item
Table.
But getting following exception,
Exception :
>>> rw_item
<class 'dp.digital_publisher.models.rw_item'>
>>> rw_item.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 67, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 82, in __len__
self._result_cache.extend(self._iter)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 271, in iterator
for row in compiler.results_iter():
File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 677, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 732, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 15, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
return self.cursor.execute(query, args)
DatabaseError: column digital_publisher_rw_item.FK_Project_id does not exist
LINE 1: ..."digital_publisher_rw_item"."deletion_date_time", "digital_p...
^
Django models.py:
FK_Project = models.ForeignKey(Project_Master, null=True, blank=True)
How to handle this?
Since you are adding fields manually, instead of using Django's database migrations , as noted by Shang Wang, you just need to make sure that the field names match up. In this case, take note of the exact error given by Django:
DatabaseError: column digital_publisher_rw_item.FK_Project_id does not exist
It is looking for FK_Project_id
, as opposed to the FK_Project
, which you added. This is because of a convention in Django, which appends _id
to the end of ForeignKey columns. So, you have two options for handling this:
Rename the column in the database: ALTER TABLE digital_publisher_rw_item RENAME COLUMN FK_Project TO FK_Project_id
Tell Django to use the column you created: FK_Project = models.ForeignKey(Project_Master, null=True, blank=True, db_column='FK_Project')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.