django unittest with option --keepdb

14 views
Skip to first unread message

麻吉柯省

unread,
Mar 5, 2020, 5:15:50 PM3/5/20
to Django users

I'm trying to use Django to create unit test cases.(I use PyMySQL==0.9.3) When I run

python manage.py test -k

Then I got an exception:
Got an error creating the test database: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE DATABASE IF NOT EXISTS `test_t` ;\n                    SET sql_no' at line 2")

I found Django use this code to avoid "database exists" warning: (django/backends/mysql/creation.py)

            if keepdb:
                # If the database should be kept, add "IF NOT EXISTS" to avoid
                # "database exists" error, also temporarily disable "database
                # exists" warning.
                cursor.execute('''
                    SET @_tmp_sql_notes := @@sql_notes, sql_notes = 0;
                    CREATE DATABASE IF NOT EXISTS %(dbname)s %(suffix)s;
                    SET sql_notes = @_tmp_sql_notes;
                ''' % parameters)
PyMySQL doesn't support those SQL,but when I use mysqlclient, it works well.What should I do?
Reply all
Reply to author
Forward
0 new messages