![](/img/trans.png)
[英]Django Foreign Key of Auth.User - column “user_id” does not exist
[英]Column does not exist error when adding Foreign Key Django
我似乎無法解決這個問題。 我在django中有一堆模型然后在我加載一些數據之后我決定在兩個模型中添加一個外鍵。 我運行了schemamigration,我被告知需要指定一個默認值,我指定了''。 遷移工作,但現在我嘗試使用其中一個表時,我得到一個錯誤。
“列myapp_mytable.myforiegnkey_id不存在”。
我刷新了數據庫,刪除了所有表,刪除了所有的遷移文件,但沒有一個可行。
以下是兩個不起作用的模型的相關代碼。
class TLOQuery(models.Model):
searchData = models.ForeignKey(Search, blank=True, null=True)
class TLOPersonSearchOutput(models.Model):
searchQuery= models.ForeignKey(TLOQuery, blank=True, null=True)
注意:還有一些其他錯誤消息。 最初它告訴我,我無法改變字段myforiengkey,因為它不存在。 它還告訴我整個表不存在。 但是,由於刷新數據庫,刪除所有表並刪除所有遷移,每當我嘗試在表上調用某些命令時,我都會收到上述錯誤
print(myModel1.objects.all())
任何建議將不勝感激!
編輯:這是實際的消息。
In [5]: print (TLOQuery.objects.all())
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
<ipython-input-5-634eb4f16f42> in <module>()
----> 1 print (TLOQuery.objects.all())
/usr/local/lib/python2.7/dist-packages/django/db/models/query.pyc in __repr__(self)
69
70 def __repr__(self):
---> 71 data = list(self[:REPR_OUTPUT_SIZE + 1])
72 if len(data) > REPR_OUTPUT_SIZE:
73 data[-1] = "...(remaining elements truncated)..."
/usr/local/lib/python2.7/dist-packages/django/db/models/query.pyc in __iter__(self)
94 - Responsible for turning the rows into model objects.
95 """
---> 96 self._fetch_all()
97 return iter(self._result_cache)
98
/usr/local/lib/python2.7/dist-packages/django/db/models/query.pyc in _fetch_all(self)
852 def _fetch_all(self):
853 if self._result_cache is None:
--> 854 self._result_cache = list(self.iterator())
855 if self._prefetch_related_lookups and not self._prefetch_done:
856 self._prefetch_related_objects()
/usr/local/lib/python2.7/dist-packages/django/db/models/query.pyc in iterator(self)
218 klass_info = get_klass_info(model, max_depth=max_depth,
219 requested=requested, only_load=only_load)
--> 220 for row in compiler.results_iter():
221 if fill_cache:
222 obj, _ = get_cached_row(row, index_start, db, klass_info,
/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.pyc in results_iter(self)
708 fields = None
709 has_aggregate_select = bool(self.query.aggregate_select)
--> 710 for rows in self.execute_sql(MULTI):
711 for row in rows:
712 if has_aggregate_select:
/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.pyc in execute_sql(self, result_type)
779
780 cursor = self.connection.cursor()
--> 781 cursor.execute(sql, params)
782
783 if not result_type:
/usr/local/lib/python2.7/dist-packages/django/db/backends/util.pyc in execute(self, sql, params)
67 start = time()
68 try:
---> 69 return super(CursorDebugWrapper, self).execute(sql, params)
70 finally:
71 stop = time()
/usr/local/lib/python2.7/dist-packages/django/db/backends/util.pyc in execute(self, sql, params)
51 return self.cursor.execute(sql)
52 else:
---> 53 return self.cursor.execute(sql, params)
54
55 def executemany(self, sql, param_list):
/usr/local/lib/python2.7/dist-packages/django/db/utils.pyc in __exit__(self, exc_type, exc_value, traceback)
97 if dj_exc_type not in (DataError, IntegrityError):
98 self.wrapper.errors_occurred = True
---> 99 six.reraise(dj_exc_type, dj_exc_value, traceback)
100
101 def __call__(self, func):
/usr/local/lib/python2.7/dist-packages/django/db/backends/util.pyc in execute(self, sql, params)
51 return self.cursor.execute(sql)
52 else:
---> 53 return self.cursor.execute(sql, params)
54
55 def executemany(self, sql, param_list):
ProgrammingError: column icesl_tloquery.searchData_id does not exist
LINE 1: SELECT "icesl_tloquery"."id", "icesl_tloquery"."searchData_i...
似乎每當我在此后不久提出問題時,我都會找到解決方案。
所以為了解決這個問題,我首先加載了所有舊的遷移,然后嘗試遷移回其工作狀態。
./manage.py migrate <app_name> 0094
當我這樣做時,它開始向后遷移,但在0096時破了。我得到的錯誤是:
FATAL ERROR - The following SQL query failed: ALTER TABLE "icesl_tloquery" ADD COLUMN "searchData_id" integer NULL DEFAULT None;
在堆棧跟蹤結束時,它說:
column "none" does not exist
然后我修改了模型以添加默認值:
class TLOQuery(models.Model):
searchData = models.ForeignKey(Search, blank=True, null=True, default=0)
然后我遷移了它再次工作。 我認為問題是當我有數據時我試圖添加一個外鍵,我提供的初始默認值是''這是一個不是整數的字符串。 所以它打破了它。 我認為我應該做的是清空所有數據,使默認值為Integer,然后遷移表。
它現在雖然有效!! 謝謝您的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.