Opened 4 years ago

Closed 4 years ago

#30824 closed Bug (invalid)

Test Cases for Multi-db and unmanged models are failing.

Reported by: seenureddy Owned by: nobody
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I was trying to write the test case for un-managed models. There's no migrations for these un-managed models. I have explained the my setup in details in this stack overflow question:


class TestModelA(models.Model):
    testid = models.CharField(max_length=200)
    class Meta:
        managed = False
        db_table = 'TestD'


import pytest

from django.db import connection, utils
from apps.models import (

from django.test import RequestFactory, TestCase
from apps.views import (

class TestDetailsTest(TestCase):
    def setUp(self):
        with connection.schema_editor() as editor:
                editor.sql_delete_table = "DROP TABLE IF EXISTS TestD;"
                editor.sql_create_table = "CREATE TABLE TestD(testid);"
                print("schema", editor.create_model(TestD))
            except utils.NotSupportedError:
        self.request = RequestFactory().get('/test_details/')
        self.get_app_details_mock = self.setup_mock('apps.get_test_details')

    def test_details(self):
        Test the app_details for successful
        :return: None
        response = test_details(self.request)
        self.assertEqual(response.status_code, 200)

        # calling the test_details method

I was able to fix the table issue by using the schema editor. Now I was getting the different issue.

======================================================================                                                                                                      ERROR: test_app_details (apps.tests.TestDetailsTest)                                                                                         ----------------------------------------
Traceback (most recent call last):                                                                                                                                            
File "C:\apps\tests\", line 23, in setUp                                                      
with connection.schema_editor() as editor:                                                                                                                                
File "C:\lib\site-packages\django\db\backends\sqlite3\", line 24, in __enter__                                                             'SQLite schema editor cannot be used while foreign key '                                                                                                                django.db.utils.NotSupportedError: SQLite schema editor cannot be used while foreign key constraint checks are enabled. Make sure to disable them before entering a transaction.atomic() context because SQLite does not support disabling them in the middle of a multi-statement transaction.    

How to fix the issue?

Change History (2)

comment:1 Changed 4 years ago by seenureddy

Component: UncategorizedTesting framework

comment:2 Changed 4 years ago by Mariusz Felisiak

Resolution: invalid
Status: newclosed
Summary: Test Cases for Multi-db and unmanged models - Test case are failingTest Cases for Multi-db and unmanged models are failing.
Type: UncategorizedBug
Version: 2.2master

It seems that disable_constraint_checking() couldn't effectively turn off foreign key constraints (see comment).

Please don't use the ticket system for help with support questions.

Closing per TicketClosingReasons/UseSupportChannels.

Note: See TracTickets for help on using tickets.
Back to Top