python - Table does not exist on testing a read-only table (Django) -


i have django project. there 2 databases in project , i've written router make 1 of them readonly. i've written unit tests use readonly database, when run python manage.py test says

programmingerror: (1146, "table 'test_arzesh-db.company' doesn't exist")

here settings of databases:

databases = {     'default': {         'engine': 'django.db.backends.mysql',         'name': 'broker-website',         'user': 'root',         'password': '',         'host': 'localhost',         'options': {             "init_command": "set foreign_key_checks = 0;",         },     },     'arzesh-db': {         'engine': 'django.db.backends.mysql',         'name': 'arzesh-db',         'user': 'root',         'password': '',         'test_database': 'default',     },     'test': {         'charset': 'utf8',         'coalation': 'utf8-unicode-ci',     } } 

and here code of router:

class router(object):     """     router control database operations on models in     auth application.     """     def db_for_read(self, model, **hints):         """         attempts read auth models go auth_db.         """         if model._meta.app_label == 'arzesh':             return 'arzesh-db'         return 'default'      def db_for_write(self, model, **hints):         """         attempts write auth models go auth_db.         """         if model._meta.app_label == 'arzesh':             return 'arzesh-db'         return 'default'      def allow_relation(self, obj1, obj2, **hints):         """         allow relations if model in auth app involved.         """         return true      def allow_migrate(self, db, app_label, model=none, **hints):         """         make sure auth app appears in 'auth_db'         database.         """         if app_label == 'arzesh':             return false         return true 

here model in readonly database:

class company(models.model):     def __unicode__(self):         return self.tick      id = models.autofield(primary_key=true)     tick = models.charfield(unique=true, max_length=32)     name = models.charfield(max_length=128, unique=true)      class meta:         managed = false         db_table = 'company' 

and here line gets error in unittest:

company.objects.create(id=1, tick='a', name='a') 

first of it's inaccurate say,

i've written unit tests use readonly database

that because

tests require database (namely, model tests) not use “real” (production) database. separate, blank databases created tests. ... default test database names created prepending test_ value of each name in databases

ref: https://docs.djangoproject.com/en/1.9/topics/testing/overview/#the-test-database

this confirmed fact error is

programmingerror: (1146, "table 'test_arzesh-db.company' doesn't exist")

right, how test database created? based on contents of migrations. have

    class meta:         managed = false         db_table = 'company' 

the managed = false here means no migration created, table not exist in test database. hence error.

solution. manually add migration runsql creates table. use sql show create table company that. first do

./manage.py makemigrations myapp --empty 

and edit newly created migrations file add runpython or runsql code it. sql pass runsql sql generated show create tbable company in mysql console. please refer runsql docs additional information , samples.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -