Django Postgres ArrayField makemigrations - python

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.

Related

Python makemigrations does not work right

I use Django framework
This is my models.py
from django.db import models
# Create your models here.
class Destination(models.Model):
name: models.CharField(max_length=100)
img: models.ImageField(upload_to='pics')
desc: models.TextField
price: models.IntegerField
offer: models.BooleanField(default=False)
and here is my migrations folder-0001_initial.py:
# Generated by Django 4.1.3 on 2022-11-17 10:17
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Destination',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
it don't migrate my model, i deleted 0001_initial.py and pycache and migrate again but it worked the same
How can i make migrations with my models ?
You have added model fields in incorrect way. You can't add like this.
change this:
class Destination(models.Model):
name: models.CharField(max_length=100)
img: models.ImageField(upload_to='pics')
desc: models.TextField
price: models.IntegerField
offer: models.BooleanField(default=False)
To this:
class Destination(models.Model):
name = models.CharField(max_length=100)
img = models.ImageField(upload_to='pics')
desc = models.TextField()
price = models.IntegerField()
offer = models.BooleanField(default=False)
After changing above code. Try to migrate manually using below commands:
python manage.py makemigrations appname
python manage.py sqlmigrate appname 0001 #This value will generate after makemigrations. it can be 0001 or 0002 or more
python manage.py migrate

Django Migration - Migration missing some fields

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()

Django makemigrations is creating migrations for model with managed = False

While Django documentation https://docs.djangoproject.com/en/3.1/ref/models/options/#managed
mentions the use of managed = False field in meta is used to not create migrations
I am still getting migrations when I call makemigrations.
This is the meta of the model:
class FieldOpsBooking(models.Model):
.
.
class Meta:
managed = False
db_table = 'field_ops_booking'
And I am getting this after makemigrations
python manage.py makemigrations
Migrations for 'user_analysis':
user_analysis/migrations/0001_initial.py
- Create model FieldOpsBooking
- Create model RewardManagementLeads
Migrations for 'od_engagement':
od_engagement/migrations/0001_initial.py
- Create model NormalisedTonnage
And it creates 0001_initial.py file with all migrations to apply as well.
Any help is appreciated
I checked my own projetcs with models having managed=False: YES there is an entry in migrations file like:
operations = [
migrations.CreateModel(
name='xyz',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
options={
'db_table': 'xyz_table',
'managed': False,
},
),
BUT: this will not create a new table on database level when you execute "makemigration".
Sorry if a insiste but your original solution was absolutely correct!!
this is from django documentation about abtract base class:
" ... since it is an abstract base class. .... and cannot be instantiated or saved directly."
You can set abstract=True in Meta to prevent model from migration as follows:
class FieldOpsBooking(models.Model):
.....
class Meta:
abstract = True
.....

Django nullable ForeignKey causes ProgrammingError

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')),
],
),
]```

Django 2.1 creates autotable

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.

Categories

Resources