添加外键Django时列不存在错误
Column does not exist error when adding Foreign Key Django
我似乎无法解决这个问题。我在 django 中有一堆模型,然后在加载一些数据后,我决定向其中两个模型添加一个外键。我 运行 schemamigration,我被告知我需要指定一个默认值,我指定了''。迁移成功了,但现在每当我尝试使用其中一个 table 时,我都会收到错误消息。
"Column myapp_mytable.myforiegnkey_id does not exist"。
我刷新了数据库,删除了所有 table,删除了所有迁移文件,none 成功了。
下面是两个模型不起作用的相关代码。
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,因为它不存在。它还告诉我整个 table 不存在。但是自从刷新数据库,删除所有 tables 并删除所有迁移后,每当我尝试在 table 上调用某些命令(如
时,我都会收到上述错误
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)
然后我迁移了,它又能用了。我认为问题是我在有数据时尝试添加外键,而我提供的初始默认值是 '',这是一个字符串而不是整数。所以它打破了它。我认为我应该做的是清空所有数据,将默认值设置为整数,然后迁移 table。
现在可以了!!感谢您的帮助!
我似乎无法解决这个问题。我在 django 中有一堆模型,然后在加载一些数据后,我决定向其中两个模型添加一个外键。我 运行 schemamigration,我被告知我需要指定一个默认值,我指定了''。迁移成功了,但现在每当我尝试使用其中一个 table 时,我都会收到错误消息。
"Column myapp_mytable.myforiegnkey_id does not exist"。
我刷新了数据库,删除了所有 table,删除了所有迁移文件,none 成功了。
下面是两个模型不起作用的相关代码。
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,因为它不存在。它还告诉我整个 table 不存在。但是自从刷新数据库,删除所有 tables 并删除所有迁移后,每当我尝试在 table 上调用某些命令(如
时,我都会收到上述错误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)
然后我迁移了,它又能用了。我认为问题是我在有数据时尝试添加外键,而我提供的初始默认值是 '',这是一个字符串而不是整数。所以它打破了它。我认为我应该做的是清空所有数据,将默认值设置为整数,然后迁移 table。
现在可以了!!感谢您的帮助!