Django makemigration error in only a table - django-models

I have a django 1.8 project and I use so many models. I decide to create a new column in an specific model but it doesn't work because "the column 'x' doesn't exists". When I try the same in other model it works fine, is just in that model. I could drop that model and create it again, but I need to fix it without erase anything. Any idea?
column = models.CharField(max_length=50, null=True)
python3 manage.py makemigrations
... django.db.utils.ProgrammingError: column table.column doesn't exists

Finally I could update my model by editing the migration.py file like:
class Migration(migrations.Migration):
dependencies = [
('appName', '000X_auto_...'),
]
operations = [
migrations.AddField(
model_name='modelName',
name='column',
field=models.CharField(max_length=50, null=True),
),

Related

why my new table cannot be added to my database

I decide to add new Model called Project to my project:
When I run python manage.py migrate, it shows me the below error:
class Project(models.Model):
statut_juridique=[
('per', 'personne physique' ),
('sarl', 'SARL'),
('sual', 'SUARL'),
('anony', 'SA'),
]
type_du_projet = [
('ind', 'industrie'),
('agr', 'agronome'),
('ser', 'service'),
('art', 'artisanat'),
('com', 'commerce'),
]
name = models.CharField(max_length=50)
produit = ArrayField(
ArrayField(
models.CharField(max_length=20, blank=True),
size=8,
),
size=8,
)
stat_jur = models.CharField(max_length=50, choices=statut_juridique)
type_projet = models.CharField(max_length=50, choices=type_du_projet)
Nomination = models.CharField(max_length=50)
adresse = models.CharField(max_length=200)
user = models.ForeignKey(User, related_name='projet', on_delete=models.CASCADE)
def __str__(self):
return self.name
Operations to perform: Apply all migrations: admin, auth,
businesplan, contenttypes, sessions Running migrations: Applying
contenttypes.0001_initial...Traceback (most recent call last): File
"/home/abdallah/projectdjango/oasis/venv/lib/python3.8/site-packages/django/db/backends/utils.py",
line 87, in _execute
return self.cursor.execute(sql) psycopg2.errors.DuplicateTable: relation "django_content_type" already exists
And also I can't see the new table in my Database, Can you help me please!
You probably use a database that already has some tables with their migrations.
For this case you can Try using a new database, or reset your existing database to remove duplicate tables or sometimes you can troubleshot using --fake-initial as one of django-admin cmd option:
$ python manage.py migrate --fake-initial
From Django-Doc:
Allows Django to skip an app’s initial migration if all database tables with the names of all models created by all CreateModel operations in that migration already exist. This option is intended for use when first running migrations against a database that preexisted the use of migrations. This option does not, however, check for matching database schema beyond matching table names and so is only safe to use if you are confident that your existing schema matches what is recorded in your initial migration.
Source: django-admin#cmdoption-migrate-fake-initial [django-doc]

Django migrate column is not the same data type as referencing column

I have the below model which is an existing DB model and through Django's inspectdb management command the below model is created.
class ExistingLegacyModel(models.Model):
period = models.TextField(db_column="Period", blank=True, null=True)
key = models.AutoField(db_column="OutlookKey", primary_key=True)
class Meta:
managed = False
db_table = "table_name"
and currently, I'm trying to create a model with a field foreign key reference to the existing legacy DB model
class TestModel(models.Model):
period = models.ForeignKey(
ExistingLegacyModel,
on_delete=models.CASCADE,
db_column="OutlookKey",
)
so when I run the makemigrations command the migration file is successfully getting created with no issue. below is the migration file content.
class Migration(migrations.Migration):
initial = True
dependencies = [
('historical', '0011_phoenixcontractprice'),
]
operations = [
migrations.CreateModel(
name='TestModel',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('period', models.ForeignKey(db_column='OutlookKey', on_delete=django.db.models.deletion.CASCADE, to='app.ExistingLegacyModel')),
],
),
]
so now when i run the migrate command now, it is failing and giving the below error.
django.db.utils.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Column 'table_name.OutlookKey' is not the same data type as referencing column 'version_testmodel.OutlookKey' in foreign key 'version_testmodel_OutlookKey_eb16c31c_fk_table_name_OutlookKey'. (1778) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Could not create constraint or index. See previous errors. (1750)")
I'm stuck with this issue for the past couple of days and I searched all over the internet but didn't get any resolution. I found a couple of StackOverflow questions that are very similar to my issue, but those questions are also unanswered.
Django - Migration foreign key field type not matching current type
Django 3.2 update AutoField to BigAutoField backward compatibility with foreign key relations
I'm currently using Django 3.2.13 and mssql-django to connect to the MSSQL database.
Any help on this will be highly appreciated! Thank you in advance.
UPDATE 1
I ran the sqlmigrate command for the initial migration. So for the period column, it is creating the table with a foreign key field with big int [OutlookKey] bigint NOT NULL whereas the existing legacy model has a normal integer field.
ALTER TABLE [<app>_<model>] ADD CONSTRAINT [<app>_<model>_OutlookKey_3505d410_fk_<existing_legacy_table>_OutlookKey] FOREIGN KEY ([OutlookKey]) REFERENCES [<existing_legacy_table>] ([OutlookKey]);

DRF created_by updated_by fail during migrations

I want to add an created_by and updated_by field to all my DB objects. I created a common model for this that will be used by most other objects. I have sorted out most obstacles so far. But the make migrations script ends with an error:
My model:
class CommonModel(models.Model):
"""Common fields that are shared among all models."""
created_by = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.PROTECT,
editable=False, related_name="+")
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.PROTECT,
editable=False, related_name="+")
created_at = models.DateTimeField(auto_now_add=True,
editable=False)
updated_at = models.DateTimeField(auto_now=True,
editable=False)
class Meta:
abstract = True
class Tag(CommonModel):
"""Tag to be used for device type"""
name = models.CharField(max_length=255)
def __str__(self):
return self.name
The error I get is :
You are trying to add a non-nullable field 'created_by' to devicetype without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
Provide a one-off default now (will be set on all existing rows with a null value for this column)
Quit, and let me add a default in models.py
The only "solution" I found searching the Internet was to define default='', run the makemigrations again and then manually edit the files afterwards to remove the default=''.
I cannot believe that this is the proper way to do this and that there is no solution for this yet.
You need to set a default value for created_at and update_at, since they are not null=True.
The message you get during migration is not an error. If you want to provide a default value, select fix 1., it should show the below prompt,
Please enter the default value now, as valid Python
The datetime and ` modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
>>>
Here you can set the default value using the datetime or django.utils.timezone module.

Django postgress - multiple primary keys are not allowed error

I am running migrations on my production system which uses a Postgress database and when I run it I get this error:
django.db.utils.ProgrammingError: multiple primary keys for table "website_experience" are not allowed
But works well on my development SQL database. Here's the model I'm working with:
class Experience (models.Model):
title = models.CharField(max_length = 60)
company = models.CharField(max_length = 60)
city = models.CharField(max_length = 60)
start_date = models.DateField(blank=False, default=datetime.now)
end_date = models.DateField(blank=True, null=True)
description = models.CharField(max_length = 1000)
creative_user = ForeignKey(CreativeUserProfile, models.CASCADE)
Initially, the field creative_user (which is my extended User model) was a primary key, but changed it to be a ForeignKey to express One to Many relationship between One CreativeUser having Many work Experience.
Here is the migration before and after making the change to ForeignKey
class Migration(migrations.Migration):
dependencies = [
('website', '0003_auto_20170510_1436'),
]
operations = [
migrations.CreateModel(
name='Experience',
fields=[
('title', models.CharField(max_length=60)),
('company', models.CharField(max_length=60)),
('city', models.CharField(max_length=60)),
('startDate', models.DateField()),
('endDate', models.DateField(blank=True, null=True)),
('creative_user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='website.CreativeUserProfile')),
],
),
]
This expresses the creation of Experience model and that creative_user was primary key on model. Then after making it a ForeignKey the migration looked like:
class Migration(migrations.Migration):
dependencies = [
('website', '0004_experience'),
]
operations = [
migrations.AddField(
model_name='experience',
name='id',
field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
preserve_default=False,
),
migrations.AlterField(
model_name='experience',
name='creative_user',
field =models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='website.CreativeUserProfile'),
),
]
As I said this all works on dev but migrating on Postgress DB thinks I have multiple primary keys. Can anyone shine some light on what wrong I'm doing?
Thanks.
Maybe is a issue related to the order of migration changes. I had this in my migration file:
operations = [
migrations.AddField(
model_name='unsubscriber',
name='id',
field=models.AutoField(default=None, primary_key=True, serialize=False),
preserve_default=False,
),
migrations.AlterField(
model_name='unsubscriber',
name='phone',
field=models.IntegerField(verbose_name='Teléfono'),
),
]
In the example I wanted to change the primary_key from phone to the new field called id, as you can see this migration is trying to create the new field as PK without changing the old one.
Just changing the order to this must work:
operations = [
migrations.AlterField(
model_name='unsubscriber',
name='phone',
field=models.IntegerField(verbose_name='Teléfono'),
),
migrations.AddField(
model_name='unsubscriber',
name='id',
field=models.AutoField(default=None, primary_key=True, serialize=False),
preserve_default=False,
),
]
It solves the problem.
I hope it helps.
I had the same issue and managed to resolve it by deleting all the migration files from the point the affected table was created, and then run makemigrations and migrate.
Your migration file "0004_experience" created a oneToOneField named "creative_user" that was set as the primary key.
My guess is, Changing from a onToOne to oneToMany relationship called for creation of a new unique field (an auto increment field "id" and set it as the primary key) in the later migration, because "creative_user" was nolonger unique.
Since the latest migration depends on migrations before it, you ended up with two primary keys.
Deleting these conflicting migration files will sort you out.
I have deleted all the migration files except init.py and run migration commands again.
python manage.py makemigrations
python manage.py migrate appName
which solved my problem.

CakePHP Name convention Issue

Cakephp V. 2.5
My database table name is group_key_persons and My Model Name is GroupKeyPerson when i am writing this code to fetching the data.
$this->loadModel ( 'GroupKeyPerson' );
$data = $this->GroupKeyPerson->find('all');
pr($data);
It return an error :
Missing Database Table
Error: Table group_key_people for model GroupKeyPerson was not found in datasource default.
But i am preety sure database table is exist. but why the hell is people comes from in my table.??
The plural of "person" is "people" not "persons". So your table name should "group_key_people"

Resources