sqlite3.InterfaceError:绑定参数 4 时出错 - 可能是不受支持的类型。错误:django.db.utils.OperationalError:无法识别的令牌:“:”

问题描述 投票:0回答:1

当我尝试保存一些 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!

python django postgresql sqlite
1个回答
0
投票

我通过更改 Postgres 的 sqllit3.db 解决了这个问题。

问题的原因基本上是ArrayField。 Sqllite3无法正确处理该字段。我没有深入挖掘来找出原因。

基本上我的模型中的这条线造成了所有破坏:

ids = ArrayField(models.IntegerField(null=True, blank=True), default=list)

从 sqlite 迁移到 Posgres 解决了整个问题。

© www.soinside.com 2019 - 2024. All rights reserved.