Django Table already exist - python

Here is my Django Migration file.
When I run
python manage.py makemigrations/migrate
I get this error.
Error:-
django.db.utils.OperationalError: (1050, "Table 'tickets_duration' already exists")
I have dropped the database and running it but still get the same error.
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='Duration',
fields=[
('Id', models.UUIDField(primary_key=True, db_column=b'duration_id', default=uuid.uuid4, serialize=False, editable=False)),
('duration', models.CharField(max_length=200, db_column=b'duration')),
],
),
migrations.CreateModel(
name='ErrorCount',
fields=[
('Id', models.UUIDField(primary_key=True, db_column=b'error_id', default=uuid.uuid4, serialize=False, editable=False)),
('error', models.CharField(max_length=200, db_column=b'error')),
],
),
migrations.CreateModel(
name='OutageCaused',
fields=[
('Id', models.UUIDField(primary_key=True, db_column=b'error_id', default=uuid.uuid4, serialize=False, editable=False)),
('outage_caused', models.CharField(max_length=200, db_column=b'outage_caused')),
],
),
migrations.CreateModel(
name='Pg',
fields=[
('Id', models.UUIDField(primary_key=True, db_column=b'pg_id', default=uuid.uuid4, serialize=False, editable=False)),
('pg_cd', models.CharField(max_length=200, db_column=b'pg_cd')),
],
),
migrations.CreateModel(
name='SystemCaused',
fields=[
('Id', models.UUIDField(primary_key=True, db_column=b'error_id', default=uuid.uuid4, serialize=False, editable=False)),
('system_caused', models.CharField(max_length=200, db_column=b'system_caused')),
],
),
migrations.CreateModel(
name='Tickets',
fields=[
('ticket_num', models.CharField(max_length=100, serialize=False, primary_key=True, db_column=b'ticket_id')),
('created_dt', models.DateTimeField(db_column=b'created_dt')),
('ticket_type', models.CharField(max_length=20, db_column=b'ticket_type')),
('addt_notes', models.CharField(max_length=1000, db_column=b'addt_notes')),
('row_create_ts', models.DateTimeField(default=datetime.datetime(2016, 2, 29, 16, 58, 31, 584733))),
('row_end_ts', models.DateTimeField(default=b'9999-12-31 00:00:00.00000-00', db_column=b'row_end_ts')),
('duration', models.ManyToManyField(to='tickets.Duration')),
('error_count', models.ManyToManyField(to='tickets.ErrorCount')),
('outage_caused', models.ManyToManyField(to='tickets.OutageCaused')),

try python manage.py migrate your_app --fake. This post talks about it. Django South - table already exists.

python manage.py migrate --fake-initial should work for django 2.2

This question is already answered here
You should run this:
python manage.py migrate <appname> --fake

temporary solution may be to comment the creation of existing table(tickets_duration).
class Migration(migrations.Migration):
dependencies = [
]
operations = [
#migrations.CreateModel(
# name='Duration',
# fields=[
# ('Id', models.UUIDField(primary_key=True, db_column=b'duration_id', default=uuid.uuid4, serialize=False, editable=False)),
# ('duration', models.CharField(max_length=200, db_column=b'duration')),
# ],
#),
....
....

version:-Django 3.X
If above solution doesn't work :
python manage.py migrate <appname> --fake
If it doesn't work then have a look at the migrations folder you will find that there will be some missing changes which u have done in models.py but somehow Django is unable to capture, so find it there and again do some changes (even a small) to that model fields and then use ,
py manage.py makemigrations app_name
py manage.py migrate app_name
or
py manage.py makemigration <appname> --fake

It is an inconsistent situation of Database.
You may do the followings:
Comment out the code for your last added model.
Run makemigrations and then migrate --fake, to get the record sync at present situation.
Now, uncomment your last added model, and run makemigrations again;

I had the same problem when using sqlite3 because of the way Django creates the third join table. From the docs,
Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship. By default, this table name is generated using the name of the many-to-many field and the name of the table for the model that contains it.
So basically, the third table is created by concatenating the two table names. This doesn't work with sqlite3 because table names in sqlite3 are case insensitive.
The resolution is to add the db_table option when declaring your ManyToManyField.
models.ManyToManyField(to='tickets.Duration', db_table='_Duration')

Related

Django swappable model foreign keys

Suppose I have a reusable app, that defines a model Person and a model Invite.
A Person has a OneToOne field to AUTH_USER_MODEL and defines some basic fields (such as birthday). It is a swappable model, so that a project which uses this app can easily add other fields (such as gender, etc.)
In my reusable app, I define a setting that provides the swapping model (otherwise, a default one will be used, exactly as django.contrib.auth does it.
The Invite model has a OneToOneField to the swappable Person model and an email field. (I think, it's quite clear what this model is for). The model itself is swappable as well, but I don't think that this makes any difference for the kind of problem I am facing.
reusable app models:
class AbstractPerson(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True, related_name='person')
birthdate = models.DateField()
class Meta:
abstract = True
class Person(AbstractPerson):
class Meta(AbstractPerson.Meta):
swappable = 'REUSABLEAPP_PERSON_MODEL'
class AbstractInvite(models.Model):
email = models.EmailField()
person = models.OneToOneField(settings.REUSABLEAPP_PERSON_MODEL, on_delete=models.CASCADE, null=False, related_name='+')
class Meta:
abstract = True
class Invite(AbstractInvite):
class Meta(AbstractInvite.Meta):
swappable = 'REUSABLEAPP_INVITE_MODEL'
If I create the initial migration for my reusable app (using a dummy project and not swapping out my models), I get the following migration for my reusable app:
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Person',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('birthdate', models.DateField()),
('user', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='person', to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
'swappable': 'REUSABLEAPP_PERSON_MODEL',
},
),
migrations.CreateModel(
name='Invite',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.EmailField(max_length=254)),
('person', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='+', to=settings.REUSABLEAPP_PERSON_MODEL)),
],
options={
'abstract': False,
'swappable': 'REUSABLEAPP_INVITE_MODEL',
},
),
]
If I then include my reusable app in another project, and swap out the Person and Invite model, I get an error when running makemigrations:
ValueError: The field myreusable_app.Invite.person was declared with a lazy reference to 'tester.myperson', but app 'tester' isn't installed.
(tester is the app that defines the swapped models, obviously)
If I delete the migration from my reusable app, and run makemigrations again, it works. the created migration is almost identical to the one above, with the exception of a new dependency:
migrations.swappable_dependency(settings.REUSABLEAPP_PERSON_MODEL),
The migration created in the tester app looks as follows:
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='MyPerson',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('birthdate', models.DateField()),
('user', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='person', to=settings.AUTH_USER_MODEL)),
],
options={
'abstract': False,
},
),
migrations.CreateModel(
name='MyInvite',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.EmailField(max_length=254)),
('person', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='+', to=settings.REUSABLEAPP_PERSON_MODEL)),
],
options={
'abstract': False,
},
),
]
I have looked up what swappable_dependency actually does: It looks only which app name is defined (let's say, the setting is tester.mymodel), and creates a dependency to this apps initial migration.
Now, if I delete the created migration from my tester app, I cannot run makemigrations again, I get the same error as above.
To clarify up until this point: Everything works as desired, if I delete the initial migration (and therefore all future migrations!) from my reusable app.
The problem, as I understand it, is the following:
The reusable app has a dependency to the initial migration of the client application that defines the swapped models. But this migration does not exist yet (heck, I am trying to create it!), so makemigration failes. (running makemigrations tester does not help).
But this almost exact same thing works flawlessly when swapping out the standard User model for a custom one. Furthermore, I do not fully understand why the error message states that the tester app is not installed. It definitly is inside my INSTALLED_APPS and it is picked up by the django-ecosystem.
After a few hours, I came up with a possible (but hacky) workaround:
Remove my reusable app from INSTALLED_APPS
Create MyInvite and MyPerson in the tester app (they both inherit from django.models.Model
Create those models by running makemigrations tester
Add my reusable app to INSTALLED_APPS
Define the swap settings
Change the inheritance of my models to their respective abstract counterparts
Run makemigrations again.
This works, because the initial migration of my reusable app now can fullfill the dependency to the swapped models by looking at the initial migration of tester that defines the models with the same name that is defined in the swap variables.
But I am sure that there must be a better way to do that.
This leaves me with the following questions:
How can I handle foreign key relationships to swapped models?
Why can't I create migrations for one app without looking at the migrations of other apps?
Unfortunately, I have to answer my own question.
This package provides a public interface to the swapping API. I have learned, that looking at closed issues is sometimes more helpful than reading the open ones.
Especially #12, and the open #10 answer my question. To sum it up, what works for the AUTH_USER_MODEL setting, does not work for any other swappable model, because the lazy_reference error is ignored, when the target model is set as AUTH_USER_MODEL within django, here:
# There shouldn't be any operations pending at this point.
from django.core.checks.model_checks import _check_lazy_references
ignore = {make_model_tuple(settings.AUTH_USER_MODEL)} if ignore_swappable else set()
errors = _check_lazy_references(self, ignore=ignore)
if errors:
raise ValueError("\n".join(error.msg for error in errors))
The proposed solution would be a registry, where all swapped models are registered and then looked up, to ignore the lazy reference error. Unfortunately, there seems to be no plans from the django devs to support this, as I have not found any open issues or feature requests for that.

Make Migrations on Django

I have a Django model which looks like this
class RedUsers(BaseModel):
user_email = models.EmailField(null=True, blank=True)
user_name = models.CharField(max_length=30, null=True, blank=True)
red_id = models.CharField(max_length=30, null=True, blank=True)
active = models.BooleanField(default=False)
def __str__(self):
return self.user_email
class Meta:
verbose_name_plural = "Red Users"
I want to add a new field
activation_key = models.CharField(max_length=40, null=True, blank=True)
I already have lots of data in this model and I can't drop the table, so I need to make migration manually.
I have tried adding the model from my 0001_initial.py file without luck
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name='RedUsers',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True)),
('modified', models.DateTimeField(auto_now=True)),
('user_email', models.EmailField(blank=True, max_length=254, null=True)),
('user_name', models.CharField(blank=True, max_length=30, null=True)),
('red_id', models.CharField(blank=True, max_length=30, null=True)),
('active', models.BooleanField(default=False)),
],
options={
'verbose_name_plural': 'RED Users',
},
),
migrations.AddField(
model_name='redusers',
name='activation_key',
field=models.CharField(blank=True, max_length=40, null=True, verbose_name='activation key'),
),
]
When I run
python manage.py migrate
It says Your models have changes that are not yet reflected in a migration, and so won't be applied.
I don't know what else to do
I want to add a new field
activation_key = models.CharField(max_length=40, null=True, blank=True)
I already have lots of data in this model and I can't drop the table,
so I need to make migration manually.
You don't need to. If you run makemigrations a second time, Django will detect that you added a field, and will make an extra migration file to add that field to the model.
You thus better here remove the AddField migration object from the migration file, and run python3 manage.py makemigrations.
It will make a new file, likely something that starts with 0002_….py, and it will take the first migration as dependency.
You can then run python3 manage.py migrate, and Django will add the field at the database level, and insert the name of the migration in the migration table.

What should I do when migrate in django does not work?

I have a problem with creating models in Django.
I want to create new model, so I write code:
class Image(models.Model):
title = models.CharField(max_length=100)
image = models.ImageField()
In the next step, I did python manage.py makemigrations , python manage.py migrate. I got this result:
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('myfirstapp', '0008_delete_image'),
]
operations = [
migrations.CreateModel(
name='Image',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('image', models.ImageField(upload_to='')),
],
),
]
but I don't have access to Image's table in Django administration.
Does anyone know what should I do? Or can see an error, know of tips, or other ways to help?
This question doesn't appear to have anything to do with migrations not working.
Migrations don't make a model appear in the admin. In order to do that, you need to register your model.

Some issues with UUID while migrating from SQLite to Postgresql in Django 2.0

I am trying to migrate my Django 2.0.4 project from SQLite to PostgreSQL 10 following the steps described here, but I am having differents problems.
During the project I changed some Integer fields to UUID4 fields.
I managed to run python manage.py migrate --run-syncdb manually editing auto_increment migration file making changes of this type (see id field):
From
class Migration(migrations.Migration):
dependencies = [
('dumps', '0011_auto_20180608_1714'),
]
operations = [
migrations.CreateModel(
name='Report',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('data', models.DateTimeField(auto_now_add=True, verbose_name='Date')),
],
),
...
...
...
To
class Migration(migrations.Migration):
dependencies = [
('dumps', '0011_auto_20180608_1714'),
]
operations = [
migrations.CreateModel(
name='Report',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, verbose_name='ID')),
('data', models.DateTimeField(auto_now_add=True, verbose_name='Date')),
],
),
...
...
...
Next, I commented all auto_increment files in which there was an AlterTable on uuid fields, but when I run python manage.py loaddata datadump.json I obtain the following error:
django.db.utils.ProgrammingError: Problem installing fixture 'C:\Users\djangoproject\datadump.json': Could not load myApp.Reservation(pk=10d00b08-bf35-469f-b53f-ec28f8b6ecb3): ERROR: column "reservation_id" is integer type but the expression is uuid type
LINE 1: UPDATE "myApp_reservation" SET "reservation_id" = '066cff3c-4b...
I think the issue here is that you have old migrations which refer to the int PK field column as an AutoField() before you made the change to use a UUIDField().
You may need to leave the id field as it was (perhaps reverse back your migrations to the point at which the swithc was made), and include a new field (and thus column of type uuid) named uuid in your Report model:
class Report(models.Model)
id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
uuid = models.UUIDField(default=uuid.uuid4, editable=False, serialize=False, verbose_name='UUID')
data = models.DateTimeField(auto_now_add=True, verbose_name='Date')
...
Then re-run database migrations ... you'll likely hit some more migration errors but give me a shout and I can advise on where to go from there in the chat.
I understood where the error was.
The problem was in postgre table scheme: 'id' field had 'integer' type instead of 'uuid'. I converted it to 'uuid' and the import was successful.

no such column: Schedule_footballscore.match_played

class FootballScore(models.Model):
team = models.ForeignKey(Team, related_name='teams_football', on_delete=models.CASCADE)
match_played = models.IntegerField(default='0')
lose = models.IntegerField(default='0')
win = models.IntegerField(default='0')
Initially i have team and win field only, Now i am adding new fields match_played and lose . When i am doing python manage.py makemigrations , no change is displayed, i even tried python manage.py makemigrations (my_app_name) . i also tried all the previous answers of Stackoverflow related to this topic.
migrations.CreateModel(
name='FootballScore',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('match_played', models.IntegerField(default='0')),
('lose', models.IntegerField(default='0')),
('win', models.IntegerField(default='0')),
],
),
I have seen this migration type of thing in one of the folder, is this the migrated list, because here all my fields are mentioned? Any help will be appreciated! Thanks.
The steps needed are:
Create your model
Create an initial migration (0001_initial)
Execute the migration
...
Add new columns to the model
Create a new migration to add the columns
Execute the new migration
Based on your example, you might not have the initial migration without the match_played column.

Categories