10

I'm trying to write some unit tests and run them using manage.py test but the script can't create the django_migrations table for some reason.

Here is the full error:

Creating test database for alias 'default'... Traceback (most recent call last): File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 83, in _execute return self.cursor.execute(sql) psycopg2.ProgrammingError: no schema has been selected to create in LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA... ^ The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\recorder.py", line 55, in ensure_schema editor.create_model(self.Migration) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 298, in create_model self.execute(sql, params or None) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\schema.py", line 117, in execute cursor.execute(sql, params) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers return executor(sql, params, many, context) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute return self.cursor.execute(sql, params) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 83, in _execute return self.cursor.execute(sql) django.db.utils.ProgrammingError: no schema has been selected to create in LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA... ^ During handling of the above exception, another exception occurred: Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line utility.execute() File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 365, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\commands\test.py", line 26, in run_from_argv super().run_from_argv(argv) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv self.execute(*args, **cmd_options) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 335, in execute output = self.handle(*args, **options) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\commands\test.py", line 59, in handle failures = test_runner.run_tests(test_labels) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\test\runner.py", line 601, in run_tests old_config = self.setup_databases() File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\test\runner.py", line 548, in setup_databases self.parallel, **kwargs File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\test\utils.py", line 176, in setup_databases serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True), File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\base\creation.py", line 68, in create_test_db run_syncdb=True, File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 141, in call_command return command.execute(*args, **defaults) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 335, in execute output = self.handle(*args, **options) File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle fake_initial=fake_initial, File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\executor.py", line 91, in migrate self.recorder.ensure_schema() File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\migrations\recorder.py", line 57, in ensure_schema raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc) django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (no schema has been selected to create in LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA... ^ ) 

I tried running these GRANT statements suggested here:

grant usage on schema public to <username>; grant create on schema public to <username>; 

But I still get the same message. Any ideas how I can fix this? Is it a search_path issue maybe?

Update

~ python manage.py makemigrations users Migrations for 'users': users\migrations\0001_initial.py - Create model MyUser ~ python manage.py migrate Operations to perform: Apply all migrations: auth, contenttypes, sessions, users Running migrations: No migrations to apply. 

I tried the makemigrations before doing the migrate (duh!), but I still get the same error as before. Did the migrations even get applied? I should also mention that my models.py only has one model/table and it is not "managed" by Django.

Bueller...Bueller...Beuller...Anyone?

4
  • Did you run ./manage.py migrate appname before running test? Can you try with clean db? Commented Feb 2, 2018 at 20:12
  • Yeah, I tried the migrate statement too, but it says there are no migrations. I tried with a fresh DB and same thing. Commented Feb 2, 2018 at 22:52
  • and did you do before migrate the ./manage.py makemigrations appname Commented Feb 2, 2018 at 22:54
  • No, I did't. But I just tried it and I get the same thing. See update above. Commented Feb 2, 2018 at 23:12

5 Answers 5

8

For same error I did something different, well not different but I directly went to psql server(in my case). It happend during the development. So, I was not concious about possible data losses. Select your database. Then, create schema.

CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public; 

I got the answer from https://stackoverflow.com/a/13823560/10860596. As, I got problem because I dropped all tables.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you.. I had the same problem with dropping all tables
6

I ran into the same error and then solved it. In my case, I believe it is because that I set Django default schema to django, when running $ python manage.py test, the test function tried to migrate some tables to database test schema django, however schema django hadn't been created there yet. So the solution is either (1) to allow Django test function to search in schema public as well, or (2) to run unit test without a test database.

Judging from the accepted answer to this question, I guess my case is different from the question's. But I think both were caused by Django being not able to find schema in the search path. Hence I posted my solution here. Hope it might help similar situations.

(django-tally-QTYVOJb0) (python3.6) D:\github\django-tally>python manage.py test Creating test database for alias 'default'... Got an error creating the test database: database "test" already exists Type 'yes' if you would like to try deleting the test database 'test', or 'no' to cancel: yes Destroying old test database for alias 'default'... Traceback (most recent call last): File "C:\Users\guido\.virtualenvs\django-tally-QTYVOJb0\lib\site-packages\django\db\backends\utils.py", line 84, in _execute return self.cursor.execute(sql) psycopg2.errors.InvalidSchemaName: no schema has been selected to create in LINE 1: CREATE TABLE "django_migrations" ("id" serial NOT NULL PRIMA... 

For solution (1): The database settings in settings.py looked like this before.

if 'RDS_HOSTNAME' in os.environ: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': os.environ['RDS_DB_NAME'], 'USER': os.environ['RDS_USERNAME'], 'PASSWORD': os.environ['RDS_PASSWORD'], 'HOST': os.environ['RDS_HOSTNAME'], 'PORT': os.environ['RDS_PORT'], 'OPTIONS': { 'options': '-c search_path=django' }, 'TEST': { 'NAME': 'test', # test database name }, }, } 

It looks like this now. I added public to the search path.

if 'RDS_HOSTNAME' in os.environ: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': os.environ['RDS_DB_NAME'], 'USER': os.environ['RDS_USERNAME'], 'PASSWORD': os.environ['RDS_PASSWORD'], 'HOST': os.environ['RDS_HOSTNAME'], 'PORT': os.environ['RDS_PORT'], 'OPTIONS': { 'options': '-c search_path=django,public' }, 'TEST': { 'NAME': 'test', # test database name }, }, } 

Comments

5

I think its loo late to answer but what work for me is just simple follow following steps:

1) open the pg admin application 2)open the database you created 3)you will see schemas and then right click on it and create the schemas and named it as public then save. 4)then migrate the table from command line you will see migrations 

Comments

3

This did the trick for me.

CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO public; GRANT ALL ON SCHEMA public TO <username>; 

Comments

1

I ran into a similar issue recently and it turned out there was no default schema defined for the user/role that was trying to create the objects. To solve that issue, I did one or both of these (I can't remember which one):

  • add the search_path: ALTER ROLE <user_name> SET search_path TO schema1, schema2
  • connect to the DB with the currentSchema parameter

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.