Boolean Field Django Can't Migrate to Postgresql
Question:
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.db.utils.ProgrammingError: cannot cast type smallint to boolean
LINE 1: ...R COLUMN "isProduct" TYPE boolean USING "isProduct"::boolean
this is my 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
How to fix this error because this error prevent sessions
and contenttype
table to be migrated
Answers:
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. I suggest you do this
Make sure the settings.py is connected to PostgreSQL
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '<PostgreSQL_DBName>',
'USER': '<PostgreSQL_UserName>',
'PASSWORD': '<PostgreSQL_Password>',
'HOST': '<PostgreSQL_HOST>',
'PORT': '<PostgreSQL_PORT>',
}
}
- Delete/Drop database in your PostgreSQL
- Delete all migrations in
migrations/
python manage.py makemigrations
python manage.py migrate
**Notes
If your PostgreSQL is localhost and you’re accessing from pgAdmin
- Disabled your connection with database
- Delete/Drop Database
- The Database looks like not deleted by all you need to do is close the pgAdmin and open it again
- follow step 3 and 4 above
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.db.utils.ProgrammingError: cannot cast type smallint to boolean
LINE 1: ...R COLUMN "isProduct" TYPE boolean USING "isProduct"::boolean
this is my 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
How to fix this error because this error prevent sessions
and contenttype
table to be migrated
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. I suggest you do this
Make sure the settings.py is connected to PostgreSQL
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '<PostgreSQL_DBName>',
'USER': '<PostgreSQL_UserName>',
'PASSWORD': '<PostgreSQL_Password>',
'HOST': '<PostgreSQL_HOST>',
'PORT': '<PostgreSQL_PORT>',
}
}
- Delete/Drop database in your PostgreSQL
- Delete all migrations in
migrations/
python manage.py makemigrations
python manage.py migrate
**Notes
If your PostgreSQL is localhost and you’re accessing from pgAdmin
- Disabled your connection with database
- Delete/Drop Database
- The Database looks like not deleted by all you need to do is close the pgAdmin and open it again
- follow step 3 and 4 above