Migrating Django project from production to local SQL server - django

I have Django project which is working fine in production(using Mysql as Database). I am running Mysql server locally in my PC(using XAMPP) , i have a done appropriate changes in setting.py as shown below.
BUT when i try to run "python manage.py migrate MYAPP" i am getting an error as shown below.
Also tried different command (same error)
--> python manage.py syncdb
--> python manage.py makemigrations MYAPP
--> python manage.py runserver
..etc.,
It is not creating any table in SQL backend , any suggestion ?
Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'MYAPP',
'USER': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
ERROR
_mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'MYAPP_DB.TABLErole' doesn't exist")

Suggesting your working on a current django release: python manage.py syncdb is deprecated. Instead use python manage.py migrate to apply django's core migrations.
Than make and apply your own migrations:
python manage.py makemigrations MYAPP
python manage.py migrate
To get a list of all available and applied migrations use:
python manage.py showmigrations

Related

Heroku doesnt migrate models on Django

I just deploy my Django app to Heroku but I cant migrate my migrations to heroku. First I run :
heroku run python manage.py migrate all the migrations list as OK but when I showmigrations, none of them is migrating (all blank [ ]).
Then I try heroku run bash and migrate from there, everything seems ok even showmigrations from bash showing all of the migrations is working. I even manage to create a superuser. But when I open my admin page and log in with superuser it shows 'account.account' table does not exist and when I check showmigrations again all of the migrations are gone. I have been repeating this migration over and over and still can't figure this out.
Does anyone know what I am doing wrong here?
Edit :
I don't know if this is related but when I push my project to Heroku the first time, I'm using Postgre with this setting :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': DB_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASSWORD,
'HOST': 'localhost',
'PORT': '5432',
}
}
but when I try to run migrations it shows an error something like cant access localhost with 5432 port, it turns out that Heroku trying to access postgre in my localhost instead of using Heroku Postgres. The solution I found is to dump my db and reload it to Heroku. And since I don't know how to set that up. I just comment that postgre setting and replace it with default django setting :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
create my dbsqlite file, running migrations again then once again push it to Heroku, hoping to find an easy way out but instead ended up in this problem
I'd recommend you to push a migrated project to heroku that is
python manage.py migrate
then push that migrated project to heroku
the reason your superuser doesn't get stored while creating superuser from heroku bash is because heroku has an ephemeral drive i.e heroku clears all the modified data from orignal push after equal interval of time.
It turns out that I forgot to install django-heroku. More on this : https://devcenter.heroku.com/articles/django-app-configuration

How to migrate the second db in django

I have two databases, one is default and the other one as first what I want is made some changes in first_db and now I want to migrate.
In my settings.py:
DATABASE_ROUTERS = ('app.router.DatabaseRouter',)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'primary_name',
'USER': 'root',
'PASSWORD': 'password',
},
'first': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'secondary_name',
'USER': 'root',
'PASSWORD': 'password',
}
}
I tried the command python manage.py migrate --database=first it showed it got migrated then when i tried to do python manage.py makemigrations it said all migrations are done and when i tried to run server and trying to insert some value in some table of first it started giving me error like the column I added is not there can anyone help me.
I think the migration didn't happen actually so anyone can show me how correctly migrate when having multiple database.
Migrations are done in database named 'first'.
if you want to migrate them to default database too just in case, then run:
python3 manage.py migrate
otherwise run:
python3 manage.py migrate --fake #To fake migration in default db for resolving runtime error
And to insert some value into table:
ModelName.objects.using('first').create(id=1 .......(other_datas)
Reference

django-admin dbshell raises django.core.exceptions.ImproperlyConfigured

I know this question has been asked before, but none of them worked for me so far, so I'm going to give it a chance here.
I'm trying to use MySQL as my database in django, but when I modify the settings.py and run the command:
django-admin dbshell
I get the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES,
but settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure()
before accessing settings.
What I did:
- I'm running windows 10.
using pipenv, I create fresh virtual environment.
install django.
start new project.
edit the settings.py
in the settings.py I change the DATABASES to the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test_db',
'USER': 'root',
'PASSWORD': '****',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
also pip installed mysqlclient
Weird thing is that when I make migrations, new tables are created, which means that DB works. why doesn't the command
django-admin dbshell
work?
based on django docs, manage.py does same thing as django-admin, and also adds settings to the sys path, therefore manage.py should be used instead of django-admin
https://docs.djangoproject.com/en/2.1/ref/django-admin/

how to switch to a new database

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>

Heroku created table but when I'll migrate, he says that doesn't created

I made syncdb (Python/Django application) in Heroku and he created table south_migrationhistory,
(venv-project)username#username:~/projectapp$ heroku run python manage.py syncdb
Running `python manage.py syncdb` attached to terminal... up, run.5529
Syncing...
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Creating table south_migrationhistory
(...)
Synced:
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.sites
> django.contrib.messages
> django.contrib.staticfiles
> django.contrib.admin
> south
Not synced (use migrations):
- core
- galeria
(use ./manage.py migrate to migrate these)
but when I'll migrate application he says that table wasn't created:
(venv-project)username#username:~/projectapp$ heroku run python manage.py migrate core
Running `python manage.py migrate core` attached to terminal... up, run.7542
(...monstruous log...)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.DatabaseError: no such table: south_migrationhistory
What can be? Thanks.
EDIT:
Solved, I put in settings_local gitignore and thereby recognized the database postgres heroku.
Tested on Django 1.9
settings.py
in_heroku = False
if 'DATABASE_URL' in os.environ:
in_heroku = True
import dj_database_url
if in_heroku:
DATABASES = {'default': dj_database_url.config()}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
Then run:
heroku addons:create heroku-postgresql:hobby-dev
sudo apt-get intall postgresql libpq-dev
pip install dj-database-url psycopg2
pip freeze >requirements.txt
git add .
git commit -m 'msg'
git push heroku master
heroku run python manage.py migrate
References:
There is no more syncdb, just migrate: What should I use instead of syncdb in Django 1.9?
https://devcenter.heroku.com/articles/deploying-python
https://devcenter.heroku.com/articles/heroku-postgresql
local_settings.py
import os
SITE_ROOT = os.path.dirname(__file__)
DEBUG = True
TEMPLATE_DEBUG = DEBUG
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(SITE_ROOT, 'data.sqlite3'),
}
}
In your gitignore, add this:
project_name/local_settings.py
*.sqlite3