Bonjour à tous,

Je souhaite mettre en place un projet django multidatase.

Pour être plus explicite :
- je dispose d'une base postgresql avec (cf schema.png):
* un schéma django dans lequel je veux mettre les tables par "defaut" de django (càd auth , group etc...)
* un schéma test dans lequel je veux mettre les tables concernant mon application "Apptest"


Nom : schemas.png
Affichages : 139
Taille : 33,2 Ko

- derrière j'ai créer un fichier db_router.py pour faire les redirections nécessaire vers les schémas :

db_routers.py:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
class AuthRouter:
        """
        A router to control all database operations on models in the
        auth application.
        """
        route_app_label= {'auth', 'contenttypes', 'sessions', 'admin'}
        def db_for_read(self, model, **hints):
            """
            Attempts to read auth models go to auth_db.
            """
            if model._meta.app_label in self.route_app_label:
                return 'django'
            return None
 
        def db_for_write(self, model, **hints):
            """
            Attempts to write auth models go to auth_db.
            """
            if model._meta.app_label in self.route_app_label:
                return 'django'
            return None
 
        def allow_relation(self, obj1, obj2, **hints):
            """
            Allow relations if a model in the auth app is involved.
            """
            if obj1._meta.app_label in self.route_app_label or obj2._meta.app_label in self.route_app_label:
                return True
            return None
 
        def allow_migrate(self, db, app_label, model_name=None, **hints):
            """
            Make sure the auth app only appears in the 'auth_db'
            database.
            """
            if app_label in self.route_app_label:
                return db == 'django'
            return None
 
class ApptestRouter:
        """
        A router to control all database operations on models in the
        auth application.
        """
        route_app_label= {'Apptest'}
        def db_for_read(self, model, **hints):
            """
            Attempts to read auth models go to auth_db.
            """
            if model._meta.app_label in self.route_app_label:
                return 'test_db'
            return None
 
        def db_for_write(self, model, **hints):
            """
            Attempts to write auth models go to auth_db.
            """
            if model._meta.app_label in self.route_app_label:
                return 'test_db'
            return None
 
        def allow_relation(self, obj1, obj2, **hints):
            """
            Allow relations if a model in the auth app is involved.
            """
            if obj1._meta.app_label in self.route_app_label or obj2._meta.app_label in self.route_app_label:
                return True
            return None
 
        def allow_migrate(self, db, app_label, model_name=None, **hints):
            """
            Make sure the auth app only appears in the 'auth_db'
            database.
            """
            if app_label in self.route_app_label:
                return db == 'test_db'
            return None
settings.py :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
 
DATABASE_ROUTER = [
    'db_routers.AuthRouter',
    'db_routers.ApptestRouter',
]
 
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
 
DATABASES = {
    'django': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'OPTIONS': {
            'options': '-c search_path=django'
        },
        'NAME': 'TestingRouter',
        'USER': 'testing',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    },
    'testing_db': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'OPTIONS': {
            'options': '-c search_path=test'
        },
        'NAME': 'TestingRouter',
        'USER': 'testing',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    },
}
et j'ai fait 2 tables simples avec une foreign key :

models.py:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
class Author(models.Model):
    a_name = models.CharField(max_length=100, verbose_name="Nom")
    a_surname = models.CharField(max_length=100, verbose_name="Prenom")
    a_country = models.CharField(max_length=100, verbose_name="Nationalité")
 
    def __str__(self):
        return self.a_surname+" "+self.a_name+" "+self.a_country
 
class Book(models.Model):
    b_author = models.ForeignKey(Author,related_name="a_book_author",verbose_name="Auteur",on_delete=models.CASCADE)
    b_title = models.CharField(max_length=100, verbose_name="Titre")
    b_type = models.CharField(max_length=50, verbose_name="Type")
 
    def __str__(self):
        return self.b_title+" "+ self.b_type
Mais en voulant vérifier sur la page admin de django j'ai l'erreur suivante :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 
django.db.utils.ProgrammingError: ERREUR:  la relation « Author » n'existe pas LINE 1: SELECT COUNT(*) AS "__count" FROM "Author"
Si quelqu'un a déjà eu ce souci et peut m'éclairer je suis preneur

en vous remerciant d'avance,
Cordialement.