Change Model's column attribute - django

I have an already migrated Django model, which was created this way:
operations = [
migrations.CreateModel(
name='Victim',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.EmailField(max_length=200)),
('instagram', models.CharField(max_length=254)),
('old_password', models.CharField(blank=True, max_length=200)),
('new_password', models.CharField(blank=True, max_length=200)),
],
),
]
But now, I want to make email and instagram attribute Blank=True, but password fields make Blank=False.
What is the easiest way to do this: delete and recreate the model (data is not important) or is there a possible way to do this?

You can still change your models and run manage.py makemigrations. It will create another migration to execute the required SQL statements to alter your database schema when running manage.py migrate. This is the role of migrations.

Related

How to run migrations on specific database using call_command() in Django?

I'm just wondering what the correct syntax is for calling $ python manage.py migrate app_name --database db_name with the management.call_command() function at runtime.
So far, I have the following:
from django.core import management
from django.core.management.commands import migrate
# Migrate the core.contrib.dynamics if needed to the pre-specified database:
management.call_command(migrate.Command(), 'dynamics', '--database {}'.format(DB_NAME))
However, I get the following error at runtime when calling the above:
Cannot find a migration matching '--database default_node' from app 'dynamics'.
I'm 99% sure I'm probably calling the -- args incorrectly? Can anyone point me in the right direction with this?
The migrations for the dynamics app are as follows:
# Generated by Django 3.0.8 on 2020-07-02 14:28
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='ModelSchema',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=32, unique=True)),
('_modified', models.DateTimeField(auto_now=True)),
],
),
migrations.CreateModel(
name='FieldSchema',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=63)),
('data_type', models.CharField(choices=[('character', 'character'), ('text', 'text'), ('integer', 'integer'), ('float', 'float'), ('boolean', 'boolean'), ('date', 'date')], editable=False, max_length=16)),
('null', models.BooleanField(default=False)),
('unique', models.BooleanField(default=False)),
('max_length', models.PositiveIntegerField(null=True)),
('model_schema', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='fields', to='dynamics.ModelSchema')),
],
options={
'unique_together': {('name', 'model_schema')},
},
),
]
Considering you already ran the migrations, you can simply use
management.call_command('migrate', app_label='dynamics', database='dbname')

Creating models.UniqueConstraint in django, but the constraint is not applying

My table is defined as below.
class Role(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
to_field="email",
db_column="user",
on_delete=models.CASCADE
)
vertical = models.ForeignKey(
Verticals,
to_field="name",
db_column="vertical",
on_delete=models.CASCADE
)
product_domain = models.ForeignKey(
ProductDomains,
to_field="name",
db_column="product_domain",
null=True,
on_delete=models.CASCADE
)
class Meta:
constraints = [
models.UniqueConstraint(fields=[
'user',
'vertical',
'product_domain'
],
name='unique-permissions-per-user')
]
Here are the migrations that are generated
migrations.CreateModel(
name='Role',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('product_domain', models.ForeignKey(db_column='product_domain', null=True, on_delete=django.db.models.deletion.CASCADE, to='verticals.ProductDomains', to_field='name')),
('user', models.ForeignKey(db_column='user', on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, to_field='email')),
('vertical', models.ForeignKey(db_column='vertical', on_delete=django.db.models.deletion.CASCADE, to='verticals.Verticals', to_field='name')),
],
),
migrations.AddConstraint(
model_name='role',
constraint=models.UniqueConstraint(fields=('user', 'vertical', 'product_domain'), name='unique-permissions-per-user'),
),
Serializers for the Role model is
class RoleSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = '__all__'
Here is the interactive console for the same
Link to console image (Unable to add due to less reputation )
Here the UniqueConstraint is not working, why?
I use models.UniqueConstraint in the same project many times but it doesn't work in this case.
My configuration is
Django - 3.0.4
Django Rest Framework - 3.11.0
Database - MySql
Please help and ask if any information is missing out.
Your logs don't show any attempt to insert the same record into the DB twice, just validation.
It appears Django Rest Framework does not import constraints from UniqueConstraint, and only has code to validate with the old Meta.unique_together constraint definition.
There is a tracking issue, ModelSerializer not generating validators for constraints, on the DRF GitHub where you can follow the latest updates on adding support for UniqueConstraint validation.

PostgreSQL psycopg2 error: there is no unique constraint / InvalidForeignKey

I have the following models:
class Contact(models.Model):
email = models.EmailField()
class EventList(models.Model):
event_contacts = models.ManyToManyField(Contact, through=EventMembership)
class EventMembership(models.Model):
event_list = models.ForeignKey(EventList, null=True, on_delete=models.PROTECT)
event_contact = models.ForeignKey(Contact, null=True, blank=False, on_delete=models.PROTECT)
However, when applying migrations for EventMembership on a completely clean database I get the following error:
psycopg2.errors.InvalidForeignKey: there is no unique constraint
matching given keys for referenced table "contacts_contact"
class Migration(migrations.Migration):
initial = True
dependencies = [
('lists', '0001_initial'),
('contacts', '0002_auto_20200308_2253'),
]
operations = [
migrations.CreateModel(
name='EventMembership',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('event_contact', apps.utils.django_multitenant.fields.TenantForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='contacts.Contact')),
('event_list', apps.utils.django_multitenant.fields.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to='lists.EventList'))
]
]
Table contacts_contact clearly has a unique constraint in id as the primary key.
What could be causing this error? / How do I debug this?
You just need to do it step by step. Now you are trying to create a foreign key relationship with a table that is not in the database yet. So comment everything out except for Contact model, apply migrations, then add EventList etc. If you are relying on the fact that Contact model goes first, well, it doesn't help in this case.

Premature field validation in Django migrations

I have the following issue:
I work on a Django app that has a primary DB and now we are adding a secondary DB for syncing only some of the data in it. When I tried to run the migrations, I got an error: null value in column 'field_2' violates not-null constraint. And here is the issue:
The model:
class A(models.Model):
field_1 = models.BooleanField(default=False)
field_2 = models.BooleanField(default=True) # added later
field_3 = models.BooleanField(default=True) # added later
and 4 migrations (well, more, but those don't count):
0001 - a related model is created
0002 - model A is created and the relation is created (ForeignKey)
0003 - field_2 is added to model A
0004 - field_3 is added to model A
However when I run migrate on the new DB, the migration stops at migration 0002 with the error mentioned above:
django.db.utils.IntegrityError: null value in column "field_2" violates not-null constraint
even though the field is created in a later migration.
I don't know hot to even begin a workaround this issue.
Edit:
Migration 0001:
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name='B',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('year', models.IntegerField()),
('active', models.BooleanField(default=True)),
],
)
]
Migration 0002:
class Migration(migrations.Migration):
dependencies = ['app', '0001']
operations = [
migrations.CreateModel(
name='A',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('field_1', models.BooleanField(default=False))
],
),
migrations.AddField(
model_name='b',
name='a',
field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.SET_NULL, to='app.B'),
),
]
Migration 0003:
class Migration(migrations.Migration):
dependencies = ['app', '0002']
operations = [
migrations.AddField(
model_name='a',
name='field_2',
field=models.BooleanField(default=True),
),
]
Migration 0004:
class Migration(migrations.Migration):
dependencies = ['app', '0003']
operations = [
migrations.AddField(
model_name='a',
name='field_3',
field=models.BooleanField(default=True),
),
]
Migrated: 0001

Using UUIDField in Django keeps creating new migrations

I have a simple model::
class HasUUID(models.Model):
name = models.CharField(max_length=10)
batchid = models.UUIDField(default=uuid.uuid4(), unique=True)
running makemigrations gives me the migration::
operations = [
migrations.CreateModel(
name='HasUUID',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=10)),
('batchid', models.UUIDField(default=uuid.UUID('79a2d9fe-e1d0-4d4b-884f-fad0bfb14f0f'), unique=True)),
],
),
]
Running migrate gives me the new table no problem. But I can run makemigrations again and I get::
operations = [
migrations.AlterField(
model_name='hasuuid',
name='batchid',
field=models.UUIDField(default=uuid.UUID('3b96231c-5848-430b-aa90-b6e41b11fd0a'), unique=True),
),
]
and while I have lived with this for a while, manually removing the unnecessary code, I need to resolve it.
so I think, make the default a separate function in the migrations as shown in various examples::
def create_uuid(apps, schema_editor):
m = apps.get_model('web', 'HasUUID')
for inst in m.objects.all():
inst.batchid = uuid.uuid4()
inst.save()
...
migrations.CreateModel(
name='HasUUID',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=10)),
('batchid', models.UUIDField(blank=True, null=True)),
],
),
migrations.RunPython(create_uuid),
migrations.AlterField(
model_name='hasuuid2',
name='batchid',
field=models.UUIDField(default=uuid.uuid4, unique=True)
),
same problem. So I tried making the default a separate function in the model::
def create_uuid():
return uuid.uuid4()
class HasUUID2(models.Model):
name = models.CharField(max_length=10)
batchid = models.UUIDField(default=create_uuid(), unique=True)
and this gets me this migration::
migrations.CreateModel(
name='HasUUID3',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=10)),
('batchid', models.UUIDField(default=uuid.UUID('335c3651-b04e-4ed8-a91d-f2da3f53dd8f'), unique=True)),
],
),
and again, keeps generating new migrations. I've also tried without the unique = True.
I'm out of ideas. There must be some settings or code elsewhere as I have used UUID fields before and I cannot find a similar problem on stackoverflow. Any suggestions?
You should pass the callable as default, not the result of a call, like:
class HasUUID(models.Model):
name = models.CharField(max_length=10)
batchid = models.UUIDField(default=uuid.uuid4, unique=True)
Notice that there are no paratheses here to make the call, we thus pass a reference to the uuid4 function itself.
The default= value is not a specific UUID (that is determined when you start the server), it should be a value that is determined when you create a new object (without specifying the batchid yourself).
By passing a callable, Django will understand that the default is the result of a call to the callable, and it will encode that in the migration. By calling the function, you retrieve the result of the call, and each time you run makemigrations, Django will think that you changed your mind on what should be the default value (it will first think you want to use '3b96231c-5848-430b-aa90-b6e41b11fd0a' as default, and later that you want to use '335c3651-b04e-4ed8-a91d-f2da3f53dd8f'). By passing a callable, the value you pass as default remains the same.