Skip to content

Instantly share code, notes, and snippets.

@Andrew-Chen-Wang
Created August 23, 2020 20:53
Show Gist options
  • Save Andrew-Chen-Wang/a3e3e8cf0902f8859ab61de025ed15a0 to your computer and use it in GitHub Desktop.
Save Andrew-Chen-Wang/a3e3e8cf0902f8859ab61de025ed15a0 to your computer and use it in GitHub Desktop.
Cast Postgres Boolean type to Small int - demonstration for Django
# Generated by Django 3.1 on 2020-08-23 20:44
"""
The SQL statement provided will convert a boolean type in PostgreSQL
to numeric int and then change that to a smallint type. I'm not sure
why I couldn't use small int for blah::smallint::numeric(1,0)...
1 is True and 0 is False
"""
from django.db import migrations, models
SQL = 'ALTER TABLE public_blah ALTER COLUMN "blah" TYPE numeric(1,0) USING blah::int::numeric(1,0);'
class Migration(migrations.Migration):
dependencies = [
('public', '0001_initial'),
]
operations = [
migrations.RunSQL(SQL),
migrations.AlterField(
model_name='blah',
name='blah',
field=models.PositiveSmallIntegerField(),
),
]
from django.db import models
# Create your models here.
class Blah(models.Model):
blah = models.PositiveSmallIntegerField()
"""
It used to be
class Blah(models.Model):
blah = models.BooleanField()
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment