django.db.utils.OperationalError: (1824, "Failed to open the referenced table 'auth_user'")
The auth dbRouter is for the [auth, sessions, contenttypes, admin] apps while the Redford dbRouter is for the main app.
Below is the code for the routers :
auth dbRouter
class AuthRouter:
"""
A router to control all database operations on models in the
auth and contenttypes applications.
"""
route_app_labels = ('auth', 'contenttypes', 'sessions', 'admin')
def db_for_read(self, model, **hints):
"""
Attempts to read auth and contenttypes models go to auth_db.
"""
if model._meta.app_label in self.route_app_labels:
return "default"
return False
def db_for_write(self, model, **hints):
"""
Attempts to write auth and contenttypes models go to auth_db.
"""
if model._meta.app_label in self.route_app_labels:
return "default"
return False
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the auth or contenttypes apps is
involved.
"""
if (obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label == 'julia' or 'redford'
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth and contenttypes apps only appear in the
'auth_db' database.
"""
if db == 'default':
# Migrate Django core app models if current database is home
if app_label in ('auth','admin','sessions','contenttypes'):
return True
else:
return False
# Non Django core app models should not be migrated if database is home
# Other database should not migrate Django core app models
elif app_label in ['auth','admin','sessions','contenttypes']:
return False
# Otherwise no opinion, defer to other routers or default database
return None
Redford dbRouter
class RedfordRouter(object):
route_app_labels = ('auth', 'contenttypes', 'sessions', 'admin')
def db_for_read(self, model, **hints):
if model._meta.app_label == 'redford':
return 'redford_db'
return False
def db_for_write(self, model, **hints):
if model._meta.app_label == 'redford':
return 'redford_db'
return False
def allow_relation(self, obj1, obj2, **hints):
if obj1._meta.app_label == 'redford' or obj2._meta.app_label == 'auth':
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if db == 'default' or app_label in self.route_app_labels:
print(db, app_label)
return None
elif db == 'redford_db':
if app_label == 'redford':
return True
return False
return None
The code for the models is as below :
class Employer(models.Model):
emp_Name = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
emp_Company = models.CharField(max_length=100)
emp_Address = models.CharField(max_length=500)
emp_Tel = models.CharField(max_length=100)
emp_JobTitle = models.CharField(max_length=200, null=True)
emp_JobDesc = models.CharField(max_length=600, null=False)
def __str__(self):
return self.emp_Name
Hope we can work it out
regards
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABOHK3QsWztSogPx_RzbYcZ46WyK0cHV8JxXBuRSbr2epeQKhg%40mail.gmail.com.
No comments:
Post a Comment