当我尝试保存一些Django模型时,比如这个:
class UserProfile(models.Model):
address = models.CharField(max_length=100, blank=True)
payment_type = models.CharField(max_length=20, default="Undefined")
total_no_of_products = models.IntegerField(default=0)
total_paid = models.FloatField(default=0)
ids = ArrayField(models.IntegerField(null=True, blank=True), default=list)
user = models.OneToOneField(NewUser, on_delete=models.CASCADE, default=None)
metadata = models.ManyToManyField(Metadata)
def __str__(self):
return f"{self.user.user_name}"
字符串
在Django shell中:
>>>profil1 = UserProfile(address="1111", payment_type="credit", total_no_of_products=10, total_paid=100, user=user1)
>>> profil1.save()
型
返回以下错误:
Traceback (most recent call last):
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py", line 105, in _execute
return self.cursor.execute(sql, params)
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return super().execute(query, params)
sqlite3.OperationalError: unrecognized token: ":"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py", line 134, in debug_sql
yield
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py", line 122, in execute
return super().execute(sql, params)
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py", line 79, in execute
return self._execute_with_wrappers(
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py", line 100, in _execute
with self.db.wrap_database_errors:
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py", line 105, in _execute
return self.cursor.execute(sql, params)
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return super().execute(query, params)
django.db.utils.OperationalError: unrecognized token: ":"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/models/base.py", line 814, in save
self.save_base(
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/models/base.py", line 901, in save_base
updated = self._save_table(
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/models/base.py", line 1059, in _save_table
results = self._do_insert(
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/models/base.py", line 1100, in _do_insert
return manager._insert(
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/models/query.py", line 1845, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1822, in execute_sql
cursor.execute(sql, params)
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py", line 121, in execute
with self.debug_sql(sql, params, use_last_executed_query=True):
File "/usr/lib/python3.10/contextlib.py", line 153, in __exit__
self.gen.throw(typ, value, traceback)
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py", line 139, in debug_sql
sql = self.db.ops.last_executed_query(self.cursor, sql, params)
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/sqlite3/operations.py", line 176, in last_executed_query
params = self._quote_params_for_last_executed_query(params)
File "/home/ilija/.cache/pypoetry/virtualenvs/django-db-IyYlsnQa-py3.10/lib/python3.10/site-packages/django/db/backends/sqlite3/operations.py", line 165, in _quote_params_for_last_executed_query
return cursor.execute(sql, params).fetchone()
sqlite3.InterfaceError: Error binding parameter 4 - probably unsupported type.
型
我试图找出是什么原因导致这个问题,我发现,当我注解了这一行,
ids = ArrayField(models.IntegerField(null=True, blank=True), default=list)
型
问题消失了。有人知道为什么吗?
目前我使用的是Django 5.0和默认的sqlite3 db。
Tnx!
1条答案
按热度按时间kx7yvsdv1#
我通过将sqllit3.db更改为Postgres解决了这个问题。
问题的原因基本上是ArrayField。Sqllite3不能正确处理这个字段。我没有深入挖掘以找出原因。
基本上,我的模型中的这条线造成了所有的破坏:
第一个月
从sqlite到Posgres解决了整个问题。