简体   繁体   English

Boolean 字段 Django 无法迁移到 Postgresql

[英]Boolean Field Django Can't Migrate to Postgresql

Django Version 4.1 I have created migration in SQLite3 and want to change to Postgresql when i connect to Postgresql i've this error Django 版本 4.1 我在 SQLite3 中创建了迁移,并希望在我连接到 Postgresql 时更改为 Postgresql 我有这个错误

django.db.utils.ProgrammingError: cannot cast type smallint to boolean
LINE 1: ...R COLUMN "isProduct" TYPE boolean USING "isProduct"::boolean

this is my model这是我的 model

from django.db import models

class Item(models.Model):
    name = models.CharField(max_length = 150)
    slug = models.SlugField(unique = True, db_index=True, null=True)
    pic = models.URLField(max_length = 400)
    address = models.CharField(max_length = 150)
    phone = models.CharField(max_length = 15)
    price = models.IntegerField()
    original_link = models.URLField(max_length = 400)
    description = models.TextField(max_length = 1500)
    additional_desc = models.TextField(max_length = 1500,default='')
    material = models.TextField(max_length = 200, default = '')
    weight = models.DecimalField(max_digits = 12, decimal_places = 2, default = 0)
    weight_unit = models.CharField(max_length = 4, default = '')
    color = models.CharField(max_length = 50, default = '')
    dimension_length = models.DecimalField(max_digits = 12, decimal_places = 2, default = 0)
    dimension_width = models.DecimalField(max_digits = 12, decimal_places = 2, default = 0)
    dimension_height = models.DecimalField(max_digits = 12, decimal_places = 2, default = 0)
    dimension_unit = models.CharField(max_length = 4, default='')
    isProduct = models.BooleanField(default = True)
    furniture_location = models.CharField(max_length = 100, default='')
    
    def __str__(self):
        return self.name

i've tried to change BooleanField to SmallIntegerField , PositiveSmallIntegerField , and IntegerField and the error still says the same thing我试图将BooleanField更改为SmallIntegerFieldPositiveSmallIntegerFieldIntegerField并且错误仍然说同样的话

How to fix this error because this error prevent sessions and contenttype table to be migrated如何修复此错误,因为此错误会阻止迁移sessionscontenttype

In my experience, if you tried to change something in Django models and the error stays the same it means you need to restart or delete the table and start migrating it.根据我的经验,如果您尝试更改 Django 模型中的某些内容并且错误保持不变,则意味着您需要重新启动或删除表并开始迁移它。 I suggest you do this Make sure the settings.py is connected to PostgreSQL我建议你这样做确保 settings.py 连接到 PostgreSQL

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '<PostgreSQL_DBName>',
        'USER': '<PostgreSQL_UserName>',
        'PASSWORD': '<PostgreSQL_Password>',
        'HOST': '<PostgreSQL_HOST>',
        'PORT': '<PostgreSQL_PORT>',
    }
}
  1. Delete/Drop database in your PostgreSQL删除/删除 PostgreSQL 中的数据库
  2. Delete all migrations in migrations/删除migrations/中的所有迁移
  3. python manage.py makemigrations
  4. python manage.py migrate

**Notes If your PostgreSQL is localhost and you're accessing from pgAdmin **注意如果您的 PostgreSQL 是 localhost 并且您正在从 pgAdmin 访问

  1. Disabled your connection with database禁用与数据库的连接
  2. Delete/Drop Database删除/删除数据库
  3. The Database looks like not deleted by all you need to do is close the pgAdmin and open it again您需要做的就是关闭 pgAdmin 并再次打开它,数据库看起来并没有被删除
  4. follow step 3 and 4 above按照上面的步骤 3 和 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM