Skip to content

Instantly share code, notes, and snippets.

@karolyi
Created November 4, 2017 01:10
Show Gist options
  • Save karolyi/54b32117addef94632a798ab97ce1eda to your computer and use it in GitHub Desktop.
Save karolyi/54b32117addef94632a798ab97ce1eda to your computer and use it in GitHub Desktop.
Modify column in django migration
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-10-19 13:31
from __future__ import unicode_literals
from django.conf import settings
from django.db import connection, migrations, models
from django.db.migrations import RunPython
PROJECT_NAMESPACE = settings.PROJECT_NAMESPACE
def forward(apps, schema_editor):
"""Modify the `approve_token` to `utf8mb4_bin` collation."""
NotifyEmail = apps.get_model(f'{PROJECT_NAMESPACE}_webshop', 'NotifyEmail')
db_table = NotifyEmail._meta.db_table
field_name = NotifyEmail._meta.get_field('approve_token').column
field_size = NotifyEmail._meta.get_field('approve_token').max_length
cursor = connection.cursor()
cursor.execute(
f'ALTER TABLE {db_table} MODIFY {field_name} '
f'VARCHAR({field_size}) BINARY CHARACTER SET utf8mb4 COLLATE '
'utf8mb4_bin NOT NULL')
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='NotifyEmail',
fields=[
('id', models.AutoField(
auto_created=True, primary_key=True, serialize=False,
verbose_name='ID')),
('first_name', models.CharField(
max_length=171, verbose_name='First name')),
('last_name', models.CharField(
max_length=171, verbose_name='Last name')),
('email', models.EmailField(
max_length=171, unique=True, verbose_name='Email')),
('is_activated', models.BooleanField(
default=False, verbose_name='Is activated')),
('approve_token', models.CharField(
max_length=50, unique=True, verbose_name='Approve token')),
('added_at', models.DateTimeField(
auto_now_add=True, verbose_name='Added at')),
],
options={
'verbose_name': 'Notify email',
'verbose_name_plural': 'Notify emails',
},
),
RunPython(code=forward)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment