This problem is driving me nuts - I can't spot what's going on so I was hoping that someone would be able to point out what I'm missing here.
I've got two sample models, FootballClub, FootballPitch, as defined below.
class FootballClub(models.Model)
name = models.CharField(max_length=100)
class Meta:
ordering = ['name']
def __unicode__(self):
return self.name
class FootballPitch(models.Model):
name = models.CharField(max_length=100)
owning_club = models.ForeignKey(FootballClub)
I've made a modification to the FootballPitch class, more specifically the owning_club field, to add null=True to it, so the class now looks like this;
class FootballPitch(models.Model):
name = models.CharField(max_length=100)
owning_club = models.ForeignKey(FootballClub, null=True)
(I don't need to manage this model through a form so I don't care that blank=True is not set).
I have also run makemigrations and migrate on this model.
When trying to list out all instances of FootballPitch that have owning_club set to null (using FootballPitch.objects.filter(owning_club__isnull=True)) I get the following error;
Programming Error: relation "footballclub_footballpitch" does not exist
LINE 1: ...d", "footballclub_footballpitch"."name",FROM "footballclub_f...
Anyone have any ideas what is going wrong? (Django 1.8.18 and Python 2.7 and postgres 9.8 for reference)
Thanks in advance!
Edit: Due to request added migrations code here instead of in reply to comment requesting it;
./manage.py --list outputs the following;
[X] 0001_initial [X] 0002_auto_20191009_1409
The migration contains # -- coding: utf-8 -- from future import unicode_literals from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('football', '0001_initial'), ] operations = [ migrations.AlterField( model_name='footballpitch', name='owning_club', field=models.ForeignKey(to='football.FootballClub', null=True), ), ]
Edit++ to include initial migration
from __future__ import unicode_literals
from django.db import migrations, models
import django.utils.timezone
import django_extensions.db.fields
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='FootballClub',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('name', models.CharField(max_length=100)),
],
options={
'ordering': ['name'],
},
),
migrations.CreateModel(
name='FootballPitch',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('name', models.CharField(max_length=100)),
('owning_club', models.ForeignKey(to='football.FootballClub')),
],
),
]```
Related
This is an easy exercise, i'm a beginner about models and migration
Models.py
from django.db import models
# Create your models here.
class Flight(models.Model):
origin = models.CharField(max_length=64),
destination = models.CharField(max_length=64),
duration = models.IntegerField()
Then i'm going on my prompt and type
python manage.py makemigrations
and in migrations/0001_initial.py
# Generated by Django 3.1.7 on 2021-03-23 16:19
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Flight',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('duration', models.IntegerField()),
],
),
]
How u can see origin and destination don't migrate
How can i fix it?
remove ',' now try makemigrations
class Flight(models.Model):
origin = models.CharField(max_length=64)
destination = models.CharField(max_length=64)
duration = models.IntegerField()
I just started learning Django through the tutorial from Djangoproject.
I'm trying to migrate but it shows "No migrations to apply" in terminal where it supposed to show "Applying polls.0001_initial... OK."
And don't know where I made mistakes, could you please help?
Thank you.
[Terminal]
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
No migrations to apply.
[mysite/polls/models.py]
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
[migrations/0001_initial.py]
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Choice',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('choice_text', models.CharField(max_length=200)),
('votes', models.IntegerField(default=0)),
],
),
migrations.CreateModel(
name='Question',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('question_text', models.CharField(max_length=200)),
('pub_date', models.DateTimeField(verbose_name='date published')),
],
),
migrations.AddField(
model_name='choice',
name='question',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Question'),
),
]
https://docs.djangoproject.com/en/2.1/intro/tutorial02/
I'm using python 3.7.2 with anaconda.
It might sound dumb but did you refresh your app? I mean example: on VSCode on the file sidebar you can right click and select refresh or click on the refresh tab by the create file tab. I have had it not show my new migrations before and had to refresh to see them.
I'm looking to use the PostgreSQL-specific ArrayField in my Django project, but it doesn't show up in the migrations after I run makemigrations. Any ideas why?
Django v2.1
Postgresql v9.6.6
# Models.py
from django.db import models
from django.contrib.postgres.fields import ArrayField
class MyClassName(models.Model):
udi = ArrayField(models.CharField()),
version = models.IntegerField()
Then I run: python3 manage.py makemigrations
# 0001_initial.py
migrations.CreateModel(
name='MyClassName',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('version', models.IntegerField()),
],
),
As you can see, the field 'udi' is suspiciously missing.
The problems are a comma at the end of the ArrayField() and CharField would need max_length too.
class MyClassName(models.Model):
udi = ArrayField(models.CharField(max_length=10))
version = models.IntegerField()
Run makemigrations again and you will get a migration you want.
I'm pretty new at Django 2.1 framework.
and I am a week trying to setup the tables for my app. Settings are fine I listed my app in INSTALLED_APPS, but when I try to run manage.py migrate code it gives me one auto_table instead of the ones that was written on model file.
These are my models.
Models.py
from django.db import models
class Nome (models.Model):
titulo = models.CharField(max_length=100),
objetivo = models.CharField(max_length=100),
class Sobrenome (models.Model):
lets = models.ForeignKey(Nome, on_delete=models.CASCADE),
make = models.CharField(max_length=100),
That's what migrate code gave to me:
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Dreams',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='Wish',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
The problem is that you have ended each line in your models definitions with a comma. This makes each attribute a tuple, so it is not recognized as an actual field. Remove the commas:
class Nome (models.Model):
titulo = models.CharField(max_length=100)
objetivo = models.CharField(max_length=100)
and run makemigrations again.
I have a mixin to add an UUID to any model:
class UUIDable(models.Model):
uuid = UUIDField(db_index=True,
default=uuid.uuid4,
editable=False)
class Meta:
abstract = True
I have an existing database with Article objects inside.
I just added an uuid field to the Article class using the mixin above.
After running the migration, all my articles now have the SAME UUID.
I expected all the objects to have a different UUID.
Why?
Here is the automatically created migration file:
class Migration(migrations.Migration):
dependencies = [
('products', '0009_auto_20171218_1630'),
]
operations = [
migrations.AddField(
model_name='article',
name='uuid',
field=models.UUIDField(db_index=True, default=uuid.uuid4, editable=False),
),
]
Your migration is adding a singular value, and you'll need to alter it during your migration. See this link: "Migrations that add unique fields"
Try altering the field in the migration:
from django.db import migrations, models
import uuid
def create_uuid(apps, schema_editor):
Article = apps.get_model('products', 'Article')
for device in Article.objects.all():
device.uuid = uuid.uuid4()
device.save()
class Migration(migrations.Migration):
dependencies = [
('products', '0009_auto_20171218_1630'),
]
operations = [
migrations.AddField(
model_name='article',
name='uuid',
field=models.UUIDField(blank=True, null=True),
),
migrations.RunPython(create_uuid),
migrations.AlterField(
model_name='article',
name='uuid',
field=models.UUIDField(unique=True)
)
]