使用迁移运行pytest django会忽略数据库触发器

使用迁移运行pytest django会忽略数据库触发器,django,postgresql,pytest,database-trigger,pytest-django,Django,Postgresql,Pytest,Database Trigger,Pytest Django,我的Django应用程序依赖于带有一些触发器设置的数据库。我用于在测试数据库中为pytest runner设置触发器 @pytest.fixture(scope='session') def django_db_setup(django_db_setup, django_db_blocker): with django_db_blocker.unblock(): cur = connection.cursor() cur.execute([...]) #

我的Django应用程序依赖于带有一些触发器设置的数据库。我用于在测试数据库中为pytest runner设置触发器

@pytest.fixture(scope='session')
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        cur = connection.cursor()
        cur.execute([...])  # Set it up
我使用
--nomigrations
运行我的测试,它按预期工作。如果没有
--nomigration
(首先测试运行迁移),触发器将无法工作

试着调试这个,我已经确认了

  • 夹具已运行,因此应设置触发器
  • 在测试开始时暂停调试器中的执行,我可以确认触发器已创建并存在于测试数据库中(通过运行
    psql test\uu
    并查看
    pg\u trigger
    表)
  • 暂停设备内部的执行,我可以确认迁移在设备之前运行。因此,迁移可能会为我设置触发器,而且可能不正确,但设备会删除所有触发器并重新创建它们
  • 移除夹具并运行迁移不会产生新结果。因此,没有理由认为夹具是问题所在。这似乎只是因为正在运行迁移
  • 让我再一次强调,在不迁移的情况下运行时,测试通过了,并且在针对我的dev db运行dev服务器时测试了功能。我还可以确认它是否正常工作


    所以,我的问题是:是否有任何理由认为运行迁移应该以不同的方式进行?还是我的迁移可能会做一些模糊的事情,导致失败,即这是我自己的错?

    将触发器置于django\u db\u设置中对我来说很有效

    @pytest.fixture(scope='session')
    def django_db_setup(django_db_setup, django_db_blocker):
        with django_db_blocker.unblock():
            cur = connection.cursor()
            cur.execute('''CREATE TRIGGER search_vector_update BEFORE INSERT OR UPDATE
                ON xml_templates_template FOR EACH ROW EXECUTE PROCEDURE
                tsvector_update_trigger(search_vector, 'pg_catalog.english', name, description, info);
            ''')
    

    pytest(4.4.1)运行时带有
    --nomigrations

    任何不使用django测试框架的原因?@e4c5太慢了。pytest库的许多额外好处