djangopostgresqltravis-cigithub-ci

Error during travis ci integration in django and postgres in Github


I integrated travis in to my project where i used postgres but when i tried to test i got stuck in with an unkwown error i.e

 $ python manage.py test
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/core/management/commands/test.py", line 53, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/test/runner.py", line 629, in run_tests
    old_config = self.setup_databases(aliases=databases)
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/test/runner.py", line 554, in setup_databases
    self.parallel, **kwargs
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/test/utils.py", line 157, in setup_databases
    test_databases, mirrored_aliases = get_unique_databases_and_mirrors(aliases)
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/test/utils.py", line 258, in get_unique_databases_and_mirrors
    default_sig = connections[DEFAULT_DB_ALIAS].creation.test_db_signature()
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/db/backends/base/creation.py", line 295, in test_db_signature
    self._get_test_db_name(),
  File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/db/backends/base/creation.py", line 153, in _get_test_db_name
    return TEST_DATABASE_PREFIX + self.connection.settings_dict['NAME']
TypeError: must be str, not NoneType
The command "python manage.py test" exited with 1.

I used postgres format as below in .travis.yml file

 services:
  - mongodb
  - postgresql

 before_script:
  - sleep 15
  - psql -c 'create database myapp_test;' -U postgres

What i will do?


Solution

  • From Django's source code, TEST_DATABASE_PREFIX is defined at line 12.

    TEST_DATABASE_PREFIX = 'test_'
    

    That means self.connection.settings_dict['NAME'] is None

    From Django's document, you need to setup test db in your settings file like

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'mydatabaseuser',
        'NAME': 'mydatabase',
        'TEST': {
            'NAME': 'mytestdatabase',   <--- YOU NEED CONFIG HERE!
        },
    },