Django未创建多个数据库表

时间:2018-06-14 07:21:14

标签: django django-models django-database

我一直在研究不同的应用程序,现在决定将它们重组在一个项目中。我已经浏览了Django docs关于多个数据库和各种帖子的SO,但我无法使其正常工作。我的问题是,当我运行manage.py migrate

时,不会创建表格

到目前为止,我已尝试删除默认数据库(包含迁移历史记录)并清空每个迁移文件夹。之后,我在this帖子中发布makemigrations && migrate --fake && migrate。但是,虽然我获得了消息成功,但是没有在PostgreSQL中创建表。

Running migrations:
  Applying auth.0008_alter_user_username_max_length... FAKED
  Applying auth.0009_alter_user_last_name_max_length... FAKED
  Applying notifications.0001_initial... FAKED
  Applying sessions.0001_initial... FAKED
  Applying watson.0001_initial... FAKED

(venv) C:\Users\USER\Documents\OutilsBU>python manage.py migrate
Operations to perform:
  Apply all migrations: Chronos, Competences, Syc, admin, auth, contenttypes, notifications, sessions, watson
Running migrations:
  No migrations to apply.

如果我重新清空迁移文件夹并删除django数据库并重新运行makemigrations并在没有假步骤的情况下迁移我只是得到此错误(表未创建)。

Operations to perform:
  Apply all migrations: Chronos, Competences, Syc, admin, auth, contenttypes, notifications, sessions, watson
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying Chronos.0001_initial... OK
  Applying Competences.0001_initial... OK
  Applying Syc.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... 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...Traceback (most recent call last):
  File "C:\Users\USER\Envs\venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: ERREUR:  Relation « Chronos_echange » does not exist.

以下是我的设置配置和db_router文件。我正在使用Django 2和PostgreSQL 9.6。提前感谢您的帮助。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'django_db',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    },
    'sycoma_db':{
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'sycoma_db',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    },
    'chronos_db':{
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'chronos_db',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    },
    'skills_db':{
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'skills_db',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    },

}

DATABASE_ROUTERS = ['OutilsBU.db_router.SycRouter', 'OutilsBU.db_router.ChronosRouter', 'OutilsBU.db_router.CompetencesRouter']

class SycRouter:
    """
    A router to control all database operations on models in the
   Syc application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to readSyc models go toSyc_db.
        """
        if model._meta.app_label == 'Syc':
            return 'sycoma_db'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to writeSyc models go to sycoma_db.
        """
        if model._meta.app_label == 'Syc':
            return 'sycoma_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in theSyc app is involved.
        """
        if obj1._meta.app_label == 'Syc' or \
           obj2._meta.app_label == 'Syc':
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the Syc app only appears in the 'sycoma_db'
        database.
        """
        if app_label == 'Syc':
            return db == 'sycoma_db'
        return None

class ChronosRouter:
    """
    A router to control all database operations on models in the
   Syc application.
    """
    def db_for_read(self, model, **hints):

        if model._meta.app_label == 'Chronos':
            return 'chronos_db'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to writeSyc models go to sycoma_db.
        """
        if model._meta.app_label == 'Chronos':
            return 'chronos_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in theSyc app is involved.
        """
        if obj1._meta.app_label == 'Chronos' or \
           obj2._meta.app_label == 'Chronos':
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the Syc app only appears in the 'sycoma_db'
        database.
        """
        if app_label == 'Chronos':
            return db == 'chronos_db'
        return None

class CompetencesRouter:
    """
    A router to control all database operations on models in the
   Syc application.
    """
    def db_for_read(self, model, **hints):

        if model._meta.app_label == 'Competences':
            return 'skills_db'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to writeSyc models go to sycoma_db.
        """
        if model._meta.app_label == 'Competences':
            return 'skills_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in theSyc app is involved.
        """
        if obj1._meta.app_label == 'Competences' or \
           obj2._meta.app_label == 'Competences':
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the Syc app only appears in the 'sycoma_db'
        database.
        """
        if app_label == 'Competences':
            return db == 'skills_db'
        return None

1 个答案:

答案 0 :(得分:0)

我终于明白了!为了调试我的问题,我为每个应用程序运行了python manage.py app_n --database app_n_db

最后,事情是其中一个子应用程序模型有一个指向django默认用户模型的ForeignKey。由于我没有为它定义特定的数据库,因此它使用默认设置进行存储,因此无法从应用程序数据库进行访问。