Django "Table already exists" on testing - python

I've tried to solve the following issue:
I'm running python manage.py test to test my application.
After creating a new test_app database, I'm getting
DatabaseError: (1050, "Table 'auth_group' already exists")
I haven't installed South (it's not on the INSTALLED_APPS list), how do I solve this?

Install south is the best way for django
You should use south like this if be the first one:
python manage.py syncdb --settings= SETTINGS_NAME
python manage.py convert_to_south APP_NAME --settings= SETTINGS_NAME
python manage.py schemamigration APP_NAME --initial --settings= SETTINGS_NAME
python manage.py migrate APP_NAME --fake --settings= SETTINGS_NAME
If you made the migrations use the following sentences:
python manage.py schemamigration APP_NAME --auto
python manage.py migrate APP_NAME --settings= SETTINGS_NAME)

you should use python manage.py migrate app_name rather than python manage.py migrate.Because you had used it before

Related

manage.py command returns help text

I have a problem with manage.py - it returns 'help' instead of creating an app. I'm quite new in Django so it could be obvious.
I've created a Django project using PyCharm.
New Project../Django../Projct
So PyCharm created a directory called Projct which looks like:
Projct/
Projct/
__init__.py
settings.py
urls.py
wsgi.py
Templates/
manage.py
Now, I want to start on project so I change a name of the SQLite database and do this command:
...PycharmProjects/Projct/manage.py startapp projct
Cmd returns:
Available subcommands
[auth]
changepassword
createsuperuser
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runfcgi
shell
showmigrations
sql
sqlall
sqlclear
sqlcustom
sqldropindexes
sqlflush
sqlindexes
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
syncdb
test
testserver
validate
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver
I've already tried syncdb and it returns the same list.
Do you know where is the problem?
Try to do the following in Pycharm Ctrl+Alt+R, when the new window comes up, type in runserver and hit enter.

django: by mistake deleted migration files from production server

By mistake I deleted all the migration files from all apps on production server.
Now running python manage.py makemigrations and then python manage.py migrate raises:
"field already exists in the database"
error for each field, What should I do ?
django version: 1.7.7
If you already migrated and then deleted.Then fake will help you
python manage.py migrate --fake
you have to fake the migration, the next command may help you:
python manage.py migrate 'name_app' 'name_migration' --fake
Example:
python manage.py migrate books 005_auto_20150505 --fake

From Django1.7 onwards `migration` is into the core. Is it similar to `south`?

I am using south for migrations. Today I surprisingly found an interesting article, django1.7 support migrations.
Interestingly I found that the startapp command even creates a folder migrations.
python manage.py startapp myapp
has tree:
myapp/
admin.py
models.py
views.py
tests.py
migrations/ <-- folder migrations
I also found command ./manage.py makemigrations, is it similar to:
./manage.py schemamigration myapp --initial or ./manage.py schemamigration myapp --auto
So what about datamigration??
Also found command ./manage.py migrate, i hope it is similar to south's ./manage.py migrate command.
So there is no ./manage.py syncdb command in django1.7??
Django 1.7 migrations are successor to South migrations.
And since there are now migrations old syncdb was deprecated. Syncdb should be working still for few versions.
You can read more details what was changed in 1.7 release notes:

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.

Unable to migrate Django app with South on Heroku

I've installed South on my existing Django app. This Django app is on Heroku as well.
Without making any changes to the models, I've done the following commands locally:
manage.py schemamigration app_name --initial
manage.py migrate --fake
Then I attempted to push heroku master, and migrate there. But I receive the following:
Running migrations for app_name:
- Nothing to migrate.
- Loading initial data for notecards.
No fixtures found
I get the same message if I try to fake the Heroku migrate.
I figured, this may be ok since technically there is nothing to migrate as the db stays the same.
So I made a small change to one of the models and did the following:
manage.py schemamigration app_name --auto
manage.py migrate
I then pushed to Heroku and attempted to migrate there, and I still receive the following:
Running migrations for app_name:
- Nothing to migrate.
- Loading initial data for notecards.
No fixtures found
Any help with what I'm doing wrong would be great. THanks.
For anyone else who comes across this issue, and for my own reference. Thanks to Chris Pratt for putting me on the right track.
This was resolved by:
Locally:
recursively removing any old migrations git rm -r migrations
flushing old database (this removes all data, but for me this wasnt an issue) manage.py flush
delete migrations folder in app folder
run initial schemamigration manage.py schemamigration --initial
run fake migrate manage.py migrate --fake
push heroku master
On Heroku:
migrate heroku run python manage.py migrate app_name
Then, locally, you can run migrate --auto, push to heroku, and run migrate app_name

Categories

Resources