Get selected database inside migration when running manage.py migrate --database=database - django

I have a data migration which loads entries into my database as shown below:
def load_groups(apps, _):
Group = apps.get_model('auth', 'Group')
Group(
name='Admin'
).save()
Group(
name='Product Manager'
).save()
Group(
name='Developer'
).save()
migrations.RunPython(
code=load_groups,
)
This code works fine when running manage.py migrate.
However if I run manage.py migrate --database=copy the migrations apply to the database with the alias copy but the load_groups section attempts to save to the database alias default.
I know I can set where to save to group creation to, but I can't seem to find a way to access the name of the database alias being used.
Any ideas would be greatly appreciated.
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django_prometheus.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'copy': {
'ENGINE': 'django_prometheus.db.backends.mysql',
'NAME': 'dev',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': 3306,
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}

Related

ElephantSQL and Django not working together, atuhentication failed

I have bumped into the following problem and still don't know how to fix it. For the record I am using mac.
I would like to connect my djnago app to an elephantsql database, so I have changed the database info.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'abc',
'USER':'abc',
'PASSWORD':'password',
'HOSTS':'tai.db.elephantsql.com',
'PORT': 5432
}
}
I see my database working fine in pgAdmin 4, so there is no issue with that, but when I run
python manage.py migrate
got the following error:
django.db.utils.OperationalError: FATAL: password authentication failed for user "abc"
Do you have any tips how to go forward?
You specify the host of the database with the HOST key, not the HOSTS for the DATAbASES setting [Django-doc]:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'abc',
'USER': 'abc',
'PASSWORD': 'password',
# ↓ HOST instead of HOSTS
'HOST': 'tai.db.elephantsql.com',
'PORT': 5432
}
}

with transaction.atomic() not working for Azure SQL Database

I use django-pyodbc-azure 2.1.0.0 for the connection with an Azure SQL database which works fine.
When I understand the documentation of django-pyodbc-azure correctly, transactions should be supported.
However, this code immediately updates the row. I would expect, that the row is updated after 20 seconds.
from django.db import transaction
from myapp.models import MyModel
import time
with transaction.atomic():
MyModel.objects.filter(id=1).update(my_field='Test')
time.sleep(20)
Am I doing something wrong? Do I need to specifiy certain settings on the Azure SQL database?
When I set AUTOCOMMIT = False in my database settings, then the following code will not update the row at all.
MyModel.objects.filter(id=1).update(my_field='Test')
time.sleep(20)
transaction.commit()
My current settings.py
'azure_reporting': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'reporting_db',
'HOST': 'xxxxxx.database.windows.net',
'PORT': '',
'USER': 'xxxx#xxxxxx.database.windows.net',
'PASSWORD': 'xxxxxx',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server'
}
}
Please make sure you have set the AUTOCOMMIT=true on database settings:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': 'yourserver.com',
'PORT': '1433',
'NAME': 'your_db',
'USER': 'your_user',
'PASSWORD': 'your_pw',
'AUTOCOMMIT': True,
'OPTIONS': {
'driver': 'ODBC Driver 13 for SQL Server',
},
},
}
No error happened, the only things what we can do is recheck the configuration of django-pyodbc-azure.
As you said: When I set AUTOCOMMIT = False in my database settings, then the following code will not update the row at all.
I think the transactions should works well.
Hope this helps.

using Geodjango with oracle database

When working with GeoDjango I have a problem: when I make migrations then migrate new models the log said "no migrations to apply" and the oracle database still have no new table.
my settings.py is
DATABASES = {
"default": {
"ENGINE": "django.contrib.gis.db.backends.oracle",
"NAME":,
"USER":,
"PASSWORD":,
}
}
I need some help.
I can see you are learning, see:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.oracle',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
First you need to create a user and password in your database, Oracle and set up this information in setting.py
You can see more information here.

How to add a remote postgres database in my django app?

What I'am trying to do is to use 2 databases in my django app. One is to be accessed from a remote server. Django settings has something like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'snackvoxadmin'
},
'users': {
.....
}
}
The database user has a url like similar to this one: postgres://a78adj1he81....
You can decompose your database url and configure it like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
And the pattern for a database url is :
postgres://user:password#host:post/database
https://docs.djangoproject.com/en/1.8/ref/settings/#databases
Or you can use the package dj-database-url to directly use the database url.
E.g. from readme :
import dj_database_url
DATABASES = {'default': dj_database_url.parse('postgres://...')}
That URL presumably consists of a username, a password, and a host name/IP address. You could split them up yourself or use the dj-database-url library.

django.db.utils.OperationalError: FATAL: database "Path" doesn't exist

I'm newbie with python and django,
Im trying to setting up Django in windows7,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.path.join(BASE_DIR, 'amour'),
'USER': 'openpg',
'PASSWORD': 'openpgpwd',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
when trying to execute the server: python manage.py runserver
the below error appear :django.db.utils.OperationalError: FATAL: database "Path" doesn't exist
I already install PostgreSql 9.3 and Python 2.7.
You should pass the name of the database, not the filename. So if you created database named "amour" then setting will be:
DATABASES = {
'default': {
...
'NAME': 'amour',
...
}
}