how to switch to a new database - django

I want to deploy my django project to the production environments, and associated it with an new empty database, and I did as follows :
Create an new empty database
Updated settings.py and pointed the database name to the new one
Deleted the migrations folder under my App
Run python manage.py runserver and no errors returned
Run python manage.py makemigrations and python manage.py migrate
but only auth related tables created ( like auth_user , auth_group ... ), no databases tables created for my Apps
How should I do for this situation to move to the new database for my project?

Deleted the migrations folder under my App
This was your mistake, you deleted the migrations - including the initial migrations. So when you go to makemigrations you haven't got the initial migration available.
So you need to run makemigrations <app_name> to at least get the initial migration.
If you were to do this again, don't delete the migrations, just change the database settings and then migrate.

Firstly, you should not have deleted the migrations. Now, make all the migrations again which you have deleted.
python manage.py makemigrations app_name
Do this for all the apps of which you have deleted the migrations.
Now, add your new database to settings.py. Do not remove the old one yet. For example, if I were adding a MySQL database, I would have added the following to the DATABASES dictionary in settings.py:
'new': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'databasename',
'USER': 'databaseusername',
'PASSWORD': 'databasepassword',
'HOST': 'localhost',
'PORT': '3306',
}
I have named the database as 'new'. Now we have two databases 'default' and 'new'. Now you have to create tables in the new database by running the migrations on the new database:
python manage.py migrate --database=new
You can follow these additional steps if you want to transfer your data to the new database. First, clear the new database:
python manage.py flush --database=new
Now export data from the old database into a json file:
python manage.py dumpdata>data.json
Import this data into the new database:
python manage.py loaddata data.json --database=new
Now you can remove the 'default' database and rename the 'new' database to 'default'.
The procedure mentioned in this answer is taken from my blog.

Just check the output of python manage.py makemigrations command, if it is showing no change detected then you need to check that have you added that app in your INSTALLED_APPS = [] in settings.py file or it might be the problem because you have deleted migration folder.Because if is there any database connectivity error it will show you that while doing makemigrations.

If your database has a new name, i.e. not "default", you need to specify it to migrate:
python manage.py migrate --database <newdb>

Related

How to integrate django with existing Postgres database

To use an existing database in Django, you need to have a model for each table. But creating models for existing tables manually is just too much work. However, there's no need to do that, since Django has a builtin tool to solve this exact problem.
Reference article linked here: how-to-integrate-django-with-existing-database
Edit settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '<name>',
'USER': '<user>',
'PASSWORD': '<password>',
'HOST': '<host>',
'PORT': '<port>',
}
}
Generate models for linked existing database tables.
python manage.py inspectdb > models.py
Tweak your tables according to your preferences. Copy all tables and add them to your app models.py
Now create initial migrations for existing tables
python manage.py makemigrations
Run the migrate command to apply the migrations, Use --fake-initial option that applies the migrations where it's possible and skips the migrations where the tables are already there:
python manage.py migrate --fake-initial
At this point, any new changes to the model structure and subsequent migrations would work as if Django managed the database since its inception
Thanks to: Dima Knivets

HOw to rebuild Django tables?

I was having problems with my dB, so I dropped the tables for one of my Django apps. I then ran makemigrations for the app and tried to migrate. It said everything was up to date. It didn't re-create the dropped tables.
How can I get Django to re-build the tables?
If you only drop your app tables and need to recreate the tables again. You also need to remove all the migrations entry from you app in the django_migrations table.
After you have done that you can run migrate again. Or if you want to reset your migrations files, remove all migrations from that app and run makemigrations and migrate
You have to delete all the files in app/migrations, except __init__.py
project
app1
migrations
__init__.py # do not delete
000_initial.py # delete
urls.py
models.py
...
app2
migrations
__init__.py # do not delete
000_initial.py # delete
urls.py
models.py
...
...
Then you can run python manage.py makemigrations and python manage.py migrate

heroku createsuperuser does not create a user. Does not make entry in auth_user table

(cfehome) C:\Dev\cfehome\verojewels>heroku run python manage.py
createsuperuser
Running python manage.py createsuperuser on ⬢ verojewels... up, run.7672
(Free)
Username (leave blank to use 'u48267'): shruti
Email address: shruti.karva#gmail.com
Password:
Password (again):
Superuser created successfully.
after this also, no entry in auth_user table is made
Execute following command to initialize database.
heroku run python manage.py migrate
Also don't forged to add Heroku Addon for database and correctly configure your Django project in settings.py.
If you are using Postgres, please check if you have something like following and DATABASES['default'] is not overridden.
import dj_database_url
DATABASES['default'] = dj_database_url.config()

Django 1.8, syncdb not working, throwing a foreign key constraint error

Since I upgrade to Django 1.8 from 1.7, I have got this foreign key constraint error.
File "c:project\env\lib\site-packages\mysql_python-1.2.5-py2.7-win32.egg/MySQLdb\connections.py line 36, in defaulterrorhandler raise errorclass, errorvalue,
Django.db.utils.IntergrityError: 'Cannot add foreing key contraint
What's some wrong with django 1.8 (latest version)?
Try this
DATABASES = {
'default': {
...
'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
},
'STORAGE_ENGINE': 'MyISAM / INNODB / ETC'
}
}
Have you created migrations for all your apps? If not, you may well be hitting the problem that the database tables are being created in the wrong order, which will give you this error.
If you have an existing Django 1.7 project, then you need to create the initial migration files, and then fake the initial migration, as described here
https://docs.djangoproject.com/en/1.8/topics/migrations/#adding-migrations-to-apps
Create the migration with
$ python manage.py make migrations your_app_label
And then fake the application
$ python manage.py migrate --fake-initial your_app_label

Django: MySQL no such table: aidata.django_session

I'm running Django 1.4 on Windows 7 in Pycharm and I installed WAMP because I need to have my data in a MySQL table.
This is from setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'aidata',
'USER': 'root'
}
}
From installed_apps I uncommented:
'django.contrib.sessions'
Running manage.py syncdb does not create any tables ( even models) in my mysqldb.
I get the error when trying to acces /admin/
DatabaseError at /admin/
(1146, "Table 'aidata.django_session' doesn't exist")
Double check the db credentials
make sure you uncommented this line in your middleware:
MIDDLEWARE_CLASSES = (
....
'django.contrib.sessions.middleware.SessionMiddleware',
)
then try to python manage.py syncdb.
if you are still having issues post any output
EDIT -- NEXT CHECK:
do you have a "django_content_type" table?
if so, does that table have a "session" record?
if so, delete the session record and try to python manage.py syncdb
EDIT -- STEP 3:
now i'm guessing, post up your settings file so i can make meaningful troubleshooting attempts
Stop your server if you have one running
go into your file browser and delete the settings.pyc file
try to python manage.py syncdb
my thought is that a pyc file with the sqlLite info may be cached and not regenerating
EDIT -- STEP 4:
everything in your settings.py look ok to me. try something for me? create a new django project, don't enable the admin or add in your apps i just want to know if from scratch everything in your django install seems to be working
django-admin.py startproject testsite
do the database configuration/setup
python manage.py syncdb
let me know if the models create properly
I was running into the same problem and for me (running django 1.7 development trunk of mid-sept.2013) it helped to
remove all south migrations ([app]/migration)-directories
remove south from INSTALLED_APPS in settings.py
That might be due to the shift towards the integrated migration system in django v1.7, but I'm speculating here.