How to migrate the second db in django - 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

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

production issue: sqlite3 to postgresql migration issue in django

I am trying to migrate my django project from sqlite3 to postgresql. I have done this succesfully in development but getting errors in production.
i have used this command to create dumps:
manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json
then i changed database in my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
}
then i exclude contenttypes as follow:
python3 manage.py shell
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
quit()
but i am getting this error:
django.db.utils.IntegrityError: Problem installing fixtures: insert or update on table
"django_admin_log" violates foreign key constraint
"django_admin_log_content_type_id_c4bce8eb_fk_django_co"
DETAIL: Key (content_type_id)=(7) is not present in table "django_content_type".
please help
okay i figured out and migrated succesfully.
thanks to ankit, Willem Van Onsem and Mostafa Ghadimi
I followed Mostafa Ghadimi's answer here step by step.
also cleared cache suggested byankit

Migrating Django project from production to local SQL server

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

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/

Mezzanine: where is the database created by `manage.py createdb`

I have the following database settings in Mezzanine:
DATABASES = {
"default": {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'lucidDB',
'USER': 'lucid',
'PASSWORD': 'xxxxxxxx',
'HOST': 'localhost',
'PORT': '',
}
}
I run the command python manage.py createdb and then answer yes to the question Would you like to fake initial migrations? (yes/no):.
Note: south was installed.
My questions:
I checked in postgres (postgres# psql --> postgres=# \l), but didn't find the database lucidDB. However, the system runs OK. Where exactly is the database created?
What does it mean by fake migration?
I didn't run syncdb or makemigrations --> migrate yet, why the system worked?
You didn't find the LucidDB in postgres because it wasn't created, you need to create it with the postgres shell see here for detailed description.
Faking a migration is marking it as complete without actually changing the database schema, it will simply add an entry in the migrationhistory database, see detailed description here.
On the Mezzanine docs it is stated that createdb is a shortcut for syncdb and migrate commands, see here for detailed description.
Hope this helps!