"python manage.py syncdb" not creating tables - python

I first ran
python manage.py syncdb
and it created the database and tables for me, then I tried to add more apps, and here's what I did:
create apps by
python manage.py startapp newapp
Then I added 'newapp' to INSTALLED_APPS in setting.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'newapp',
)
At last I ran syncdb:
python manage.py syncdb
and here's the result I get:
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
I checked my db and there is no table named newapp, no table's name including newapp.

I also ran into this issue, and was able to get around it by removing the migrations folder inside my app. Somehow that had already gotten created and was tricking syncdb into thinking that it was fully migrated already, but those migration scripts didn't actually do anything useful. Obviously don't try this if you actually have migrations you want to save, but I was working with a brand new app and models.

If you run:
python manage.py inspectdb > somefile.txt
You can get quickly check out if your database structure is matching your django models.

I tried most of the ideas above:
making sure the models.py is imported (verified that the module executed during a makemigrate),
deleting the migrations folder
setting managed = True (this is default anyways),
used the python manage.py inspectdb (which correctly dumped the table, if I had created it manually).
The key was simply to run a makemigrations on the app separately:
python manage.py makemigrations <app_name>
as part of performing the makemigrations step. Then you do
python manage.py migrate
afterwards as usual.
(applies to Django 1.10, using Postgres 9.5).
Django documentation
Credit, related post

i got same problem, but i didn't have any "migration" folder. I solved it like below.
I just added app_label with the model code,
class MyModel(models.Model):
...
class Meta:
managed = True # add this
app_label = 'myapp' # & this
Also, make sure it is discoverable by referencing it in myapp/models/__init__.py
from model_file.py import MyModel

You should be using this command
python manage.py migrate
where you were runing syncdb from the location where manage.py resides

I was having the same problem and noticed that it had created a db.sqlite3 file that didn't seem empty:
$ ls -l
-rw-r--r-- 1 sauron staff 122880 Jul 31 01:22 db.sqlite3
I tried running sqlite again using the filename as an argument and that worked:
$ sqlite3 db.sqlite3
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema
CREATE TABLE "auth_group" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(80) NOT NULL UNIQUE
);
... many rows ...

Related

Django 2.2.4 - “No migrations to apply” when run migrate after makemigrations

I am trying to do a migration in django 2.2.4 in python 3.7.
First I try to make do makemigations:
python3 manage.py makemigrations
I get:
Migrations for 'main':
main/migrations/0001_initial.py
- Create model TutorialCategory
- Create model TutorialSeries
- Create model Tutorial
But then I try the second step:
python3 manage.py migrate
I get:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, main, sessions
Running migrations:
No migrations to apply.
Even though a migration should happen.
I tried deleting my migrations folder and then remaking it (with the empty __init__.py file inside) but it still doesn't work.
(Note: I have been following along the tutorial: Linking models with Foreign Keys - Django Web Development with Python p.9 by sentdex)
I faced the same problem in django 2.2, The following worked for me...
delete the migrations folder resided inside the app folder
delete the pycache folder too
restart the server [if your server is on and you are working from another cli]
python manage.py makemigrations <app_name> zero
python manage.py makemigrations <app_name> [explicit app_name is important]
python manage.py migrate
Somehow your migrations are virtually or faked applied in the database, Truncating django_migrations table should work.
Delete all the migrations files:
find . -path "/migrations/.py" -not -name "init.py" -delete
find . -path "/migrations/.pyc" -delete
Truncate table:
truncate django_migrations
makemigrations, migrate.
w/in the app directory I deleted the pycache and migrations folders,
from django.migrations tables I deleted all rows like this for PostgreSQL
DELETE FROM public.django_migrations
WHERE public.django_migrations.app = 'target_app_name';
To delete any app tables already created for the tables to be created from scratch.
Mine didn't migrate cause there was already a record in the django_migrations table with the same name, so I just removed it and then migrate worked.

I keep getting no changes detected when migrating app in Django

I am following this video tutorial to learn Django: https://www.youtube.com/watch?v=F5mRW0jo-U4&t=909s
I am at the "Your first app component" section where he creates an app and then migrates it. I have followed every one of his steps so far yet I keep getting the no changes detected error when I migrate the app
I have tried researching ways to fix this but it doesn't seem like there is any one right way to do it, it's more of a case by case basis. I've made sure my app is under the install_apps section in the settings. I've tried python manage.py makemigrations but that tells me there are no changes detected. I've tried "python manage.py makemigrations product" to be more specific but it tells me that App 'product' could not be found. Is it in INSTALLED_APPS?" even though it is in installed apps
currently this is my installed apps section:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'product',
after migrate and makemigrations command put your app name
like this
python manage.py migrate product
python manage.py makemigrations product
my case it's working
You didn't add your app name in 'Installed_Apps' in settings.py. Add this first and then run again.
for a proper migrations with 'python manage.py makemigrations' make sure that after creating your model class you save them before you typing any console code very important

Add field to existing django app in production environment

I have an existing django app and would like to add a field to a model.
But because the website is already in production, just deleting the database is not an option any more.
These are the steps I took:
pip install south
added 'south' to INSTALLED_APPS
python manage.py syncdb
python manage.py convert_to_south [myapp]
So now I have the initial migration and south will recognize the changes. Then I added the field to my model and ran:
python manage.py schemamigration [myapp] --auto
python manage.py migrate [myapp]
Now I have the following migrations:
0001_initial.py
0002_auto__add_field_myapp_fieldname.py
Which commands should I run on my production server now to migrate? Also should I install south first and then pull the code changes and migrations?
first is to fake the initial migration:
python manage.py migrate [yourapp] --fake 0001
then you can apply the migration to the db
python manage.py migrate [yourapp]
I'm assuming you ran convert_to_south on development, in which case production still wouldn't be aware of the migrations yet. convert_to_south automatically fakes the initial migration for you! If you were to just run migrate on production without faking, it should error.

NoMigrations exception for django.contrib.x has no migrations

I am trying to migrate a model that has nothing to do with this and as soon as I do migrate it gives me this error:
NoMigrations: Application '<module 'django.contrib.comments' from C:\Python27\lib\site-packages\django\contrib\comments\__init__.pyc' has no migrations.
I have tried deleting migrations folder and deleting databases but keeps coming up.
It can be that you have another app named as the last part of the module in this case 'comments' or you have an entry in the database table south_migrationhistory with app_name same as the module.
Found answer here Google Groups
i suggest if you can use django dumpdata , you will get a json file copy of your model.
$ python manage.py dumpdata <app-name> --indent=2
or create a folder to put the result in
$ python manage.py dumpdata <app-name> --indent=2 > [project]/[app]/[folder]/file-name.json
later you can use data upload to rebuild the original model.

Why don't my south migrations work?

First, I create my database.
create database mydb;
I add "south" to installed Apps. Then, I go to this tutorial: http://south.aeracode.org/docs/tutorial/part1.html
The tutorial tells me to do this:
$ py manage.py schemamigration wall --initial
>>> Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate wall
Great, now I migrate.
$ py manage.py migrate wall
But it gives me this error...
django.db.utils.DatabaseError: (1146, "Table 'fable.south_migrationhistory' doesn't exist")
So I use Google (which never works. hence my 870 questions asked on Stackoverflow), and I get this page: http://groups.google.com/group/south-users/browse_thread/thread/d4c83f821dd2ca1c
Alright, so I follow that instructions
>> Drop database mydb;
>> Create database mydb;
$ rm -rf ./wall/migrations
$ py manage.py syncdb
But when I run syncdb, Django creates a bunch of tables. Yes, it creates the south_migrationhistory table, but it also creates my app's tables.
Synced:
> django.contrib.admin
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.sites
> django.contrib.messages
> south
> fable.notification
> pagination
> timezones
> fable.wall
> mediasync
> staticfiles
> debug_toolbar
Not synced (use migrations):
-
(use ./manage.py migrate to migrate these)
Cool....now it tells me to migrate these. So, I do this:
$ py manage.py migrate wall
The app 'wall' does not appear to use migrations.
Alright, so fine. I'll add wall to initial migrations.
$ py manage.py schemamigration wall --initial
Then I migrate:
$ py manage.py migrate wall
You know what? It gives me this BS:
_mysql_exceptions.OperationalError: (1050, "Table 'wall_content' already exists")
Sorry, this is really pissing me off. Can someone help ? thanks.
How do I get South to work and sync correctly with everything? The only thing I can think of is remove my app from INSTALLED_APPS, then run syncdb, then add it back on.
That is SO SILLY.
South allows you to create migrations when you first start out with a new app and the tables haven't been added to the database yet, as well as creating migrations for legacy apps that already have tables in the database. The key is to know when to do what.
Your first mistake was when you deleted your migrations, as soon as you did that, and then ran syncdb, Django didn't know that you wanted south to manage that app anymore, so it created the tables for you. When you created your initial migrations and then ran migrate, south was trying to create tables that django already created, and thus your error.
At this point you have two options.
Delete the tables for the wall app from your database and then run $ py manage.py migrate wall This will run the migration and create your tables.
Fake out the initial migration run
$ py manage.py migrate wall 0001 --fake This will tell south that you already have the tables on the database so just fake it, which will add a row to the south_migrationhistory table, so that the next time you run a migrate it will know that the first migration has already been run.
Setting up a brand new project and no database
create your database
add south to installed apps
run syncdb, this will add the django and south tables to the database
add your apps
for each app run python manage.py schemamigration app_name --initial this will create the initial migration files for your app
then run south migrate python manage.py migrate app_name this will add the tables to the database.
Setting up a legacy project and database
add south to installed apps
run syncdb, this will add the south tables to the database
for each of your apps run python manage.py schemamigration app_name --initial This will create your initial migrations
for each of your apps run python manage.py migrate app_name 0001 --fake , this will fake out south, it won't do anything to the database for those models, it will just add records to the south_migrationhistory table so that the next time you want to create a migration, you are all set.
Setting up a legacy project and no database
create database
add south to installed apps
for each of your apps run python manage.py schemamigration app_name --initial This will create your initial migrations
run syncdb, this will add any apps that don't have migrations to the database.
then run south migrate python manage.py migrate this will run all migrations for your apps.
Now that you are setup with south, you can start using south to manage model changes to those apps. The most common command to run is python manage.py schemamigration app_name migration_name --auto that will look at the last migration you ran and it will find the changes and build out a migration file for you. Then you just need to run python manage.py migrate and it alter your database for you.
This is how I get things working.
pip install South
# add 'south', to INSTALL_APPS, then
python manage.py syncdb
# For existing project + database
python manage.py convert_to_south app_name
# Thereafter, call them per model changes
python manage.py schemamigration app_name --auto
python manage.py migrate app_name
References:
http://garmoncheg.blogspot.com/2011/08/django-how-and-why-to-use-migrations.html
http://www.djangopro.com/2011/01/django-database-migration-tool-south-explained/
The tutorial you're using states:
(If this fails complaining that
south_migrationhistory does not exist,
you forgot to run syncdb after you
installed
South.)
Assuming that your post accurately details the steps you've taken, following that link seems to show that you missed a step before setting up your new app. As you are following a tutorial for setting up migrations on a new application, the order is:
Add south to INSTALLED_APPS.
Run syncdb.
Then follow the tutorial.
I.e., you should've already run syncdb before you added in the models for your new app. Your solution of removing your app from INSTALLED_APPS should work, but it's worth noting that it's really only a "silly" work-around, as you missed a step earlier on. Had syncdb been run before you created the models for that app, you wouldn't have to use the work-around.
Just for future ref. If South is giving you any problems:
Remove the migrations directories from your app directories
Delete South_migrations from your database
Run manage.py syncdb
Go back to using South (e.g. './manage.py convert_to_south something, ./manage.py migrate ...')
This seems obvious, but I'd highly recommend reading the docs.
Even after reading the answers to this question I struggled to understand how to use South effectively.
That all changed of course the day I read the docs and you should too, South is simpler to use than you might think.
http://south.aeracode.org/docs/about.html
http://south.aeracode.org/docs/tutorial/index.html
http://south.aeracode.org/docs/convertinganapp.html#converting-an-app
I also found this useful:
http://www.djangopro.com/2011/01/django-database-migration-tool-south-explained/
And make sure you read Jeff Atwood's Coding Horror articles on database version control.
How do I get South to work and sync
correctly with everything? The only
thing I can think of is remove my app
from INSTALLED_APPS, then run syncdb,
then add it back on.
I have used that fix with South troubles in the past. Not a pretty solution but very effective ;)
But the main problem is that your order isn't correct. You should have run syncdb before the tutorial. Than it works properly.

Categories

Resources