Use Django Migrations to delete a table - python

I am moving from Rails to Django and have an existing database. I created models.py via python manage.py inspectdb > models.py, made changes, and tested some endpoints. Everything seems fine.
I then ran python manage.py makemigrations and migrate to make the initial django mirgation.
I noticed an old Rail's specific Model / table called ArInternalMetadata / ar_internal_metadata. I figure I could easily remove the table by simply removing the model from models.py and rerun makemigrations however when I do that, django just says No changes detected. Running migrate doesn't remove the table either.

Figured it out. When inspectdb creates Models from an existing database, it sets managed in the Meta inner class to False by default.
class AssetCategories(FacadeModel):
created_at = models.DateTimeField()
updated_at = models.DateTimeField()
name = models.CharField(max_length=255)
deleted_at = models.DateTimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'asset_categories'
Per the Django 2.1 Docs this will prevent deletion from happening
If False, no database table creation or deletion operations will be performed for this model
Removing managed = False from the Meta class allows makemigrations / migrate to delete the table.

Related

How to Query data from MySQL database after updating model field in Django

After updating posts model field in MySQL database using using Django framework, I'm unable to query posts from the Database. Whenever I run the command python manage.py runserver, all seems to be well with the server. However, when enter the post url to display the list of posts I uploaded on the database before updating the model field, I get 1054, "Uknown column 'start_post.author_id' in the 'field list'
I have spent a couple of hours try to figure out why I'm getting the error but I still don't get it.
In model.py I had:
....
title = models.CharField()
preamble= models.Charfield()
body = models.TextField()
....
I updated the above model to:
....
title = models.CharField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
introduction = models.charfield()
body = models.TextField()
....
Before updating it everything was working appropriately. But after updating it I'm unable to access the list of posts on browser, from the url start\ as well as the post detail page. I didn't have the author field in the first model but I added it to the updated model. What can I do to solve this problem?
After update models, you have to run the commands:
python manage.py makemigrations
python manage.py migrate
makemigrations command will check the diffs between your current model in database and model in python code.
migrate will really run the migration to reflect your model with database model.
You can find more information about makemigrations and migrate in django documentation about makemigrations and migrate.

After Add New model.How to Django Table Migrate without losing data

class UserSubStatus(models.Model):
msisdn = models.CharField(max_length=200)
category = models.CharField(max_length=200, null=True, blank=True)
validity = models.IntegerField(default=1,null=True,blank=True)
sub_status = models.IntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-created_at']
db_table = "user_sub_status"
I add this table on my model.py file. How add this "user_sub_status" table on my database without losing any data.
Hello Dear
First run the following command:
python manage.py makemigrations your_app_name
than run this command:
python manage.py migrate your_app_name
NB: I hope, "user_sub_status" will be added to your database without losing data.
run makemigrations and migrate as you did before. If you do not erase previous migrations files then by running makemigrations new migrations files will add to migrations folder and by migrate new tables or columns for previous tables will add to your database and there will be no problem with your stored data
First, you have to run makemigrations command which is responsible for creating new migrations based on the changes you have made to your models.
python manage.py makemigrations
Then you have to run a migration command which is responsible for applying and unapplying migrations that are created by the above command.
python manage.py migrate
With the help of above both command, your table "user_sub_status" will be added in your database without losing any data from your database.

Limit custom migration to one model

I have decided to implement registration option for my website, I used this tutorial (signup with confirmation part). Following this material I have created Profile module to hold some info. Everything (seems to be) working now, but the problem is that old profiles throws relatedObjectDoesNotExist error. According to these two questions (first, second) I need to make a migration to create profiles for old user accounts. I tried to follow this doc as suggested in one of the answers, but then I try to run a migration I get following error: KeyError: ('stv', 'bazinekaina')
stv is the name of my app and bazinekaina is the name of the next model after the one I need to create profiles.
How to limit migration to only the first model?
My relevant models.py code:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
email_confirmed = models.BooleanField(default=False)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField(max_length=254)
#receiver(post_save, sender=User)
def update_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.get(user=instance)
instance.profile.save()
#next model, this one throws an error, despite the fact it should not be touched at all
class BazineKaina(models.Model):
#bazines kainos modelis
bazka = models.DecimalField(max_digits=5, decimal_places=2)
data = models.DateField(auto_now=False, auto_now_add=True)
def __str__(self):
return str(self.bazka)
class Meta:
verbose_name_plural = "BazinÄ— kaina"
get_latest_by = 'data'
Migration file crated after using python manage.py makemigrations --empty stv command, named 0001_initial.py:
from django.db import migrations, models
def sukurti_profilius(apps, schema_editor):
Profile = apps.get_model("stv", "Profile")
for user in Profile.objects.all():
Profile.objects.get_or_create(user=user)
class Migration(migrations.Migration):
dependencies = [
]
operations = [
]
How and what I should fix to stop migrations from applying to the unrelated models (and throwing error)?
Sorry if it is basic question, but whole django is still very new to me.
If your migration is named 0001_initial then it means that you don't have a migration that actually creates the table for the profile model.
Remove that migration and run:
python manage.py makemigrations stv
python manage.py makemigrations stv --empty --name create_profiles
Then you should have a file 0002_create_profiles.py and put the logic to create profiles there.

Django MySQL IntegrityError: ("Field 'created_at' doesn't have a default value")

I am new to Django.
IntegrityError: (1364, "Field 'created_at' doesn't have a default value")
occurred when I didn't write created_at for models I defined at models.py.
Every time I face such error, I add
created_at = models.DateTimeField(default=timezone.now, null=True)
to models.py and then run python manage.py makemigrations; python manage.py migrate --fake.
Is that OK? (I mean, does it follow Django best practice or not? )
When I was using Rails, I've never faced such issues. Why doesn't Django automatically handle created_at column?
Also, I'd like to know why --fake option removes this error.
version
Django==1.11.5
mysqlclient==1.3.12
Python 3.6.1
Thanks in advance.
It seems that you want to have an attribute created_at which is set on every creation of a model instance. There is an easier way for that:
created_at = models.DateTimeField(auto_now_add=True)
Here the docs explaining it in detail https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.DateField.auto_now_add
From your comments I think I can reproduce your steps. You had a model class in your models.py:
from django.db import models
class YourModel(models.Model):
one_field = models.CharField()
another_field = models.CharField()
Then you ran:
python manage.py makemigrations
python manage.py migrate
After that you added to your model class:
from django.db import models
class YourModel(models.Model):
one_field = models.CharField()
another_field = models.CharField()
created_at = models.DateTimeField(default=timezone.now, null=True)
but forgot to run the migrations and got the error message.
This is because Django ORM is looking for the property created_at, which it can't find in the DB.
You have to run the commands:
python manage.py makemigrations
python manage.py migrate
again. Remember that the option --fake won't change the DB. It just marks the migration as run. Use it only when you're sure what are you doing.
As Johannes Reichard has suggested you should better use auto_now_add (and there is also auto_now) for this purpose. Check the official documentation.
The drawback is that this field won't be shown in the admin. A workaround is to overwrite the save method:
def save(self, *args, **kwargs):
if not self.pk:
self.created_at = timezone.now() # import it from django.utils
super(YourModel, self).save(*args, **kwargs)

Django Foreign Key not working

Edit:Django version 1.6.1
I keep getting this error when trying to create a RedditPost object through the admin:
no such column: subreddit_id
The subreddit_id error references RedditPost.subreddit (not Subreddit.subreddit) and then adds an _id at the end for some reason.
There's no problem with the reference. When trying to create a RedditPost from admin, the drop down menu for Subreddits shows all the available Subreddits objects.
class Subreddit(models.Model):
subreddit = models.CharField(max_length=100, primary_key=True)
title = models.CharField(max_length=100, null=False)
def __unicode__(self):
return smart_unicode(self.subreddit)
class RedditPost(models.Model):
comments_link = models.CharField(max_length=256, primary_key=True)
submitted_link = models.CharField(max_length=256, null=False)
rank = models.IntegerField(null=False)
title = models.CharField(max_length=100, null=False)
reddit_timestamp = models.DateTimeField(null=False)
updated_at = models.DateTimeField(auto_now_add=True, auto_now=True)
subreddit = models.ForeignKey('Subreddit')
RESOLVED/SOLUTION: I ended using "flush" which didn't clear up the databases when I made changes (I wasn't using any migrations). I had to use:
python manage.py sqlclear "app_name" | python manage.py dbshell
To completely clear the database and then I had to follow this link(Django South error with initial migration) to do the migration correctly.
_id is automatically added by Django. there are reasions for it.
your db table RedditPost doesnot just have this column whatsoever..
after changing the table (by adding this new column), do:
if django version < 1.7: (south needs to be installed)
python manage.py schemamigration yourapp --auto
python manage.py migrate yourapp
if django version >= 1.7:
python manage.py makemigrations
python manage.py migrate
I had the same problem. Delete your db.sqlite3 file. Then on the terminal,
python3 manage.py makemigrations
python3 manage.py migrate
That's it. But your all saved database values will be deleted.

Categories