在 Travis 上运行 django unittests 时如何解决“psycopg2.errors.UndefinedTable:关系“auth_user”不存在”
Posted
技术标签:
【中文标题】在 Travis 上运行 django unittests 时如何解决“psycopg2.errors.UndefinedTable:关系“auth_user”不存在”【英文标题】:How to resolve the "psycopg2.errors.UndefinedTable: relation "auth_user" does not exist" when running django unittests on Travis 【发布时间】:2019-10-14 18:25:39 【问题描述】:我使用 Travis for CI/CD 作为我的 Django 应用程序的一部分,并带有一个 postgresql 数据库。 (Django 2.1.4)
测试运行后,Travis 上的构建始终会失败。我收到此错误:
psycopg2.errors.UndefinedTable: relation "auth_user" does not exist
我试过了:makemigrations
、migrate auth
、migrate myapp
、migrate --run-syncdb
。所有这些都失败并出现相同的错误。
测试使用 sqlite3 数据库在本地运行,并使用 postgresql 数据库在类似于 prod 的 heroku 环境中运行。
.travis.yml
...
before script:
-psql -c 'create database travis_ci_test;' -U postgres
services:
-postgresql
script:
-yes | python3 manage.py makemigrations
-python3 manage.py migrate auth
-python3 manage.py migrate --run-syncdb
-python3 manage.py tests test/unit_tests
settings.py
...
DATABASES =
'default':
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'travis_ci_test',
'USER': 'postgres',
'PASSWORD': '',
'HOST': 'localhost',
...
INSTALLED_APPS = [...
'django.contrib.auth',
]
这是 Travis 构建失败的输出。 'migrate auth' 成功(我认为这是 auth_user 的关键行:Applying auth.0001_initial... OK
)
0.22s$ psql -c 'create database travis_ci_test;' -U postgres
CREATE DATABASE
1.50s$ yes | python3 manage.py makemigrations
TEST_ENV...
AWS_INTEGRATION...
Databases ... 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'travis_ci_test', 'USER': 'postgres', 'PASSWORD': '', 'HOST': 'localhost'
Installed Apps ... ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'races.apps.RacesConfig', 'storages']
No changes detected
The command "yes | python3 manage.py makemigrations" exited with 0.
1.68s$ python3 manage.py migrate auth
TEST_ENV...
AWS_INTEGRATION...
Databases ... 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'travis_ci_test', 'USER': 'postgres', 'PASSWORD': '', 'HOST': 'localhost'
Installed Apps ... ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'races.apps.RacesConfig', 'storages']
Operations to perform:
Apply all migrations: auth
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
The command "python3 manage.py migrate auth" exited with 0.
1.57s$ python3 manage.py migrate --run-syncdb
TEST_ENV...
AWS_INTEGRATION...
Databases ... 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'travis_ci_test', 'USER': 'postgres', 'PASSWORD': '', 'HOST': 'localhost'
Installed Apps ... ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp.apps.MyAppConfig', 'storages']
Operations to perform:
Synchronize unmigrated apps: messages, myapp, staticfiles, storages
Apply all migrations: admin, auth, contenttypes, sessions
Synchronizing apps without migrations:
Creating tables...
Creating table myapp_model1
Creating table myapp_model2
Creating table myapp_model3
Creating table myapp_model4
Creating table myapp_model5
Running deferred SQL...
Running migrations:
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying sessions.0001_initial... OK
The command "python3 manage.py migrate --run-syncdb" exited with 0.
1.40s$ python3 manage.py test tests/unit_tests
TEST_ENV...
AWS_INTEGRATION...
Databases ... 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'travis_ci_test', 'USER': 'postgres', 'PASSWORD': '', 'HOST': 'localhost'
Installed Apps ... ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app.apps.MyAppConfig', 'storages']
Creating test database for alias 'default'...
Traceback (most recent call last):
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "auth_user" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 10, 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 26, 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 316, 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 353, 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 56, 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 604, in run_tests
old_config = self.setup_databases()
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/test/runner.py", line 551, in setup_databases
self.parallel, **kwargs
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/test/utils.py", line 174, in setup_databases
serialize=connection.settings_dict.get('TEST', ).get('SERIALIZE', True),
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/db/backends/base/creation.py", line 68, in create_test_db
run_syncdb=True,
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/core/management/__init__.py", line 148, in call_command
return command.execute(*args, **defaults)
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 172, in handle
self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 310, in sync_apps
self.stdout.write(" Running deferred SQL...\n")
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 106, in __exit__
self.execute(sql)
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 133, in execute
cursor.execute(sql, params)
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "auth_user" does not exist
The command "python3 manage.py test tests/unit_tests" exited with 1.
【问题讨论】:
可能不是问题,但您不应该在这里运行 makemigrations。 Makemigrations 是您在本地执行的操作,之后您将生成的迁移文件添加到您的版本控制中。 您的数据库设置与运行测试无关,因为 Django 测试运行器将在运行时创建(并销毁)一个全新的数据库。因此,您在运行manage.py test
之前所做的与测试失败无关。阅读this。我不知道为什么你的测试失败了。您是否在模块级别访问数据库?
这里出了点问题:您发布的 INSTALLED_APPS
与输出 Installed Apps ... ['django.contrib.admin', ...
不匹配
正在使用自定义用户模型吗?
@DanielRoseman - 奇怪的是,这是解决问题的关键。由于应用程序没有迁移,删除该步骤使其失败。这让我意识到迁移文件没有被发送到版本控制。稍后迁移文件夹中的一个 init.py 文件,它正在工作。
【参考方案1】:
问题在于您的某个应用正在调用或引用一个不存在的模型。该模型可能在您的代码中,但尚未对其运行迁移。
修复:确保所有应用都有迁移包(即带有 init.py 文件的迁移文件夹),然后运行 makemigrations 并迁移。
【讨论】:
【参考方案2】:我删除了 sqllite3 数据库 然后我跑了
$ python manage.py migrate
然后
$ python manage.py migrate --run-syncdb
【讨论】:
【参考方案3】:在为已安装的应用添加模型后,我遇到了同样的问题,但忘记创建初始迁移。 OP 将答案隐藏在 cmets 中,所以这里有明确的步骤:
$ mkdir myapp/migrations
$ touch myapp/migrations/__init__.py
在此之后,我在尝试运行 python manage.py test
时遇到了一个新错误
psycopg2.errors.InvalidCursorName: cursor "_django_curs_140073820227328_58" does not exist
还需要一个步骤:
$ python manage.py makemigrations
然后测试运行良好。
【讨论】:
这似乎发生在运行单元测试时,您的模型和迁移文件之间存在任何不一致。以上是关于在 Travis 上运行 django unittests 时如何解决“psycopg2.errors.UndefinedTable:关系“auth_user”不存在”的主要内容,如果未能解决你的问题,请参考以下文章
Swift 测试在本地通过,但在 Travis-CI 上构建失败
Travis CI AVD上的Android Instrumentation测试失败,但在本地模拟器上工作
react-native IOS 发布方案上的 Travis 错误