I have a problem with using schema while running tests with django.
All tables in my postgres database are in schema, let's name it 'schema'.
In my settings.py i added:
'OPTIONS': {
'options': '-c search_path=schema',
'connect_timeout': 5,
},
to tell django to use 'schema' schema.
All production code is working perfectly.
Problem is:
When i added DB testing and run
python manage.py test
it resulted with
django.db.utils.ProgrammingError: no schema has been selected to create in
I tried to remove schema-line from settings.py and add 'schema.' at the beginning of every db_table name in models.py.
It worked for tests, but now when i run my production code, it ends up with:
django.db.utils.ProgrammingError: relation "schema.table" does not exist
How can i tell manage.py to create db with schema, so i don't have to modify models.py or settings.py?
Or how can i properly add usage of my schema in models.py ?
Related
I got this error when running command python manage.py test for testing purpose.
I tried after delete my pycache and migration files and db.sqlite3 then run the python manage.py makemigrations and python manage.py migrate again as well.
But still got the same error...
Here is my test.py
from rest_framework.test import APITestCase
from django.urls import reverse
from rest_framework import status
# Create your tests here.
class UserTest(APITestCase):
def setUp(self):
register_url = reverse('user:register')
data = {
"username":"Tester",
"email":"tester#gmail.com",
"password":"tester123",
"mobile_number":"03322917356"
}
self.client.post(register_url, data, format='json')
def test_user_can_register(self):
register_url = reverse('user:register')
data = {
"username":"Tester1",
"email":"tester1#gmail.com",
"password":"tester123",
"mobile_number":"03322911356"
}
response = self.clent.post(register_url , data , format='json')
self.assertEqual(response.status_code ,status.HTTP_201_CREATED)
Try switching to postgresql, to see if it is a database issue always good to try something else. if that doesn’t work then I would get an existing example that does work and compare it from there to what you have. tbh unless you have your code opensource it is difficult to debug something like this without speculation as a lot of different systems are affected when ensuring migrations are applied. Check your migrations files to see if those are indeed correct. they should be under /migrations
Have you added your app in settings.py to installed apps?
In hour installed app comment out django sites. Do makemigrations and migrate. Then reactivate django sites and do the migration stuff again
We are a small team, trying to work with Django with a restricted access to a futurely unmanaged PostgreSQL database (i.e: only views and stored procedures ; no access to any tables) for security reasons.
We tried to give (within Postgre) the user external_test the rights to create any tables inside his own schema on external, and to use the following settings (settings.py):
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'external': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgre_db',
'USER': 'external_user',
'PASSWORD': 'password',
'HOST': 'integration.project.net',
'PORT': '5432',
'TEST': {
'NAME': 'test_db',
'USER': 'external_test',
'PASSWORD': 'password',
...
Using simple passing unit tests (project/app/tests/test_views.py):
...
class InternalTest(TestCase):
database = ['default']
def test_something(self):
pass
class StoredProcedureTest(TestCase):
databases = ['external']
def test_one_procedure(self):
with connections["external"].cursor() as cursor:
cursor.callproc("some_procedure", [42, ])
pass
...
If we try the first one with ./manage.py test app.tests.test_views.InternalTest
→ ok
If we try the other one with ./manage.py test app.tests.test_views.StoredProcedureTest
→ circular dependency issue
(ImproperlyConfigured: Circular dependency in TEST[DEPENDENCIES])
probably because it's skipping the configuration of default
If we try both tests with ./manage.py test app.tests.test_views:
→ permission denied
Creating test database for alias 'default'...
Creating test database for alias 'external'...
Got an error creating the test database: permission denied to create database
(Django try to create a database test_db as the user external_user)
We don't really get what Django is trying to do and how to properly configure it.
If we give the rights to external_user to create their own databases:
the database test_db is created by external_user
the schema of default (sqlite) is created in test_db of external (postgre)
the schema of external (postgre) is not created in test_db
Questions
Is django able to handle this ?
What are we doing wrong ?
What is the point of specifying a user external_user for TEST if in the end django is using the normal user external_user ?
Why does it write the schema of default in test_db ? Is there a way to create only models of some apps in it ?
Why isn't it able to create the schema of external in test_db ?
I hope it was described enough. Thank you in advance for your responses =)
the django doc says to change the sites name and domain in the django.contrib.sites framework one should use a migration [1].
But they forgot to mention where I should put this migration. I tried to create a directory named "sites" and a directory named "django.contrib.sites". But no matter in which directory I put my migration, manage.py migration always says there is nothing to update.
I also tried to run python manage.py makemigrations --empty sites, but then the migration is created in the lib directory: ve/lib/python3.5/site-packages/django/contrib/sites/migrations/0003_auto_20160904_2144.py. This may be correct behaviour, but then I cannot set my change under source control.
In case something is wrong with my migration, here it is:
from __future__ import unicode_literals
from django.db import migrations, models
def set_site_name(apps, schema_editor):
Sites = apps.get_model('django.contrib.sites', 'site')
site = Sites.objects.filter(id=1).first()
if site != None:
site.name = "name"
site.domain = "name.com"
class Migration(migrations.Migration):
initial = True
operations = [
migrations.RunPython(set_site_name),
]
So my question is: where does django expect to find those migrations?
Thank you very much in advance for your help.
[1] https://docs.djangoproject.com/en/1.10/ref/contrib/sites/#enabling-the-sites-framework
Each app in a Django project must have a unique label. Naming your app sites isn't a good idea - it will clash with the django.contrib.sites app unless you change the label in the app config class.
If you have an existing app specific to your project, you could use that app to store the data migration.
Alternatively choose a different name like mysites. Create the app with ./manage.py startapp mysite, add the app to your INSTALLED_APPS, then create a blank migration.
I want to migrate from sqlite3 to MySQL in Django. First I used below command:
python manage.py dumpdata > datadump.json
then I changed the settings of my Django application and configured it with my new MySQL database. Finally, I used the following command:
python manage.py loaddata datadump.json
but I got this error :
integrityError: Problem installing fixtures: The row in table
'django_admin_log' with primary key '20' has an invalid foregin key:
django_admin_log.user_id contains a value '19' that does not have a
corresponding value in auth_user.id.
You have consistency error in your data, django_admin_log table refers to auth_user which does not exist. sqlite does not enforce foreign key constraints, but mysql does. You need to fix data and then you can import it into mysql.
I had to move my database from a postgres to a MySql-Database.
This worked for me:
Export (old machine):
python manage.py dumpdata --natural --all --indent=2 --exclude=sessions --format=xml > dump.xml
Import (new machine):
(note that for older versions of Django you'll need syncdb instead of migrate)
manage.py migrate --no-initial-data
Get SQL for resetting Database:
manage.py sqlflush
setting.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'asdf',
'USER': 'asdf',
'PASSWORD': 'asdf',
'HOST': 'localhost',
#IMPORTANT!!
'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
},
}
python manage.py loaddata dump.xml
I am trying to create a 'Reusable App'. I've written some database models, and then copy/pasted the folder (which is supposed to be my reusable app) into the Project I want to be using it in. I then added the folder's name to INSTALLED_APPS in my settings.
Then I used South to run:
python manage.py schemamigration test --initial --settings=settings_local
and:
python manage.py migrate test --settings=settings_local
When I tried accessing this app's models in the admin I got a relation does not exist. I went in my PostgreSQL and realized the tables were not created with those South commands.
Any idea what I am doing wrong?
UPDATE:
I am using Django 1.5.10 and South 0.7.5
The migration that is created when running schemma migration
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'Category'
db.create_table(u'test_category', (
(u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('name', self.gf('django.db.models.fields.CharField')(unique=True, max_length=200)),
))
db.send_create_signal(u'test', ['Category'])
def backwards(self, orm):
# Deleting model 'Category'
db.delete_table(u'test_category')
models = {
u'test.category': {
'Meta': {'object_name': 'Category'},
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '200'})
}
}
complete_apps = ['test']
I've generated the migrations and installed the app in a project of mine without a problem with a MySQL Db.
I've not really changed anything, but I did fix an import error in your form.
Your form inherits BaseInlineFormSet which you tried to import from django.forms but it should be imported from;
from django.forms.models import BaseInlineFormSet
I've zipped up the app as it is running in my project;
https://www.dropbox.com/s/tqyi9su942rsp1u/ubiwhere_games.zip?dl=0