У меня есть приложение Django, использующее две базы данных: default (созданное моим приложением django) и paretodb (устаревшая база данных, для которой я сделал модели, используя inspectdb
).
Вот маршрутизатор:
class ParetoRouter (object):
def db_for_read(self, model, **hints):
"Point all operations on buyer_ladder models to 'paretodb'"
if model._meta.app_label == 'buyer_ladder':
return 'paretodb'
return 'default'
def db_for_write(self, model, **hints):
"Point all operations on buyer_ladder models to 'paretodb'"
if model._meta.app_label == 'buyer_ladder':
return 'paretodb'
return 'default'
def allow_relation(self, obj1, obj2, **hints):
"Allow any relation if a both models in buyer_ladder app"
if obj1._meta.app_label == 'buyer_ladder' and obj2._meta.app_label == 'buyer_ladder':
return True
# Allow if neither is buyer_ladder app
elif 'buyer_ladder' not in [obj1._meta.app_label, obj2._meta.app_label]:
return True
return False
def allow_syncdb(self, db, model):
if db == 'paretodb' or model._meta.app_label == "buyer_ladder":
return False # we're not using syncdb on our legacy database
else: # but all other models/databases are fine
return True
Вот часть login_registration.models.py:
class Company(models.Model):
name = models.CharField(max_length=255)
cbsa = models.CharField(max_length=5, blank=True, null=True, default=None)
authorized_tester = models.BooleanField(default=False)
def __str__(self):
return self.name
class Meta:
app_label = "login_registration"
class User(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
email = models.CharField(max_length=255)
company = models.ForeignKey(
Company, related_name="users", on_delete=models.CASCADE, default=None
)
password = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = UserManager()
def __repr__(self):
return f"< User object: {self.first_name} {self.last_name}>"
def __str__(self):
return f"{self.first_name} {self.last_name}"
class Meta:
app_label = "login_registration"
и customer_ladder.models.py:
class CbsaMapping(models.Model):
fips = models.CharField(primary_key=True, max_length=5)
state_fips = models.CharField(max_length=255, blank=True, null=True)
county_fips = models.CharField(max_length=255, blank=True, null=True)
county_name = models.CharField(max_length=255, blank=True, null=True)
fips_class_code = models.CharField(max_length=255, blank=True, null=True)
state = models.CharField(max_length=255, blank=True, null=True)
region_general = models.CharField(max_length=255, blank=True, null=True)
region_audantic = models.CharField(max_length=255, blank=True, null=True)
division = models.CharField(max_length=255, blank=True, null=True)
county_name_experian = models.CharField(max_length=255, blank=True, null=True)
cbsa = models.CharField(max_length=5, blank=True, null=True)
metropolitan_division_code = models.CharField(max_length=255, blank=True, null=True)
csa = models.CharField(max_length=5, blank=True, null=True)
cbsa_title = models.CharField(max_length=255, blank=True, null=True)
metro_micro = models.CharField(max_length=255, blank=True, null=True)
metropolitan_division_title = models.CharField(max_length=255, blank=True, null=True)
csa_title = models.CharField(max_length=255, blank=True, null=True)
class Meta:
managed = False
db_table = 'cbsa_mapping'
class BuyerLadderProperty(models.Model):
pid = models.IntegerField()
did = models.IntegerField(primary_key=True)
recording_date = models.DateField(blank=True, null=True)
grantee = models.CharField(max_length=255, blank=True, null=True)
buyer_owner_occupied = models.IntegerField(blank=True, null=True)
situs_address = models.CharField(max_length=100, blank=True, null=True)
situs_city = models.CharField(max_length=100, blank=True, null=True)
situs_zip = models.CharField(max_length=5, blank=True, null=True)
fips = models.CharField(max_length=5, blank=True, null=True)
cbsa = models.CharField(max_length=5, blank=True, null=True)
sale_price = models.IntegerField(blank=True, null=True)
discount = models.FloatField(blank=True, null=True)
estimated_value_high = models.IntegerField(blank=True, null=True)
estimated_value_low = models.IntegerField(blank=True, null=True)
year_built = models.IntegerField(blank=True, null=True)
square_footage = models.IntegerField(blank=True, null=True)
bedrooms = models.IntegerField(blank=True, null=True)
bathrooms = models.FloatField(blank=True, null=True)
multi_parcel = models.CharField(max_length=255, blank=True, null=True)
lender_name = models.CharField(max_length=255, blank=True, null=True)
document_type_description = models.CharField(max_length=255, blank=True, null=True)
buyer_id = models.IntegerField(blank=True, null=True)
address_line_1 = models.CharField(max_length=255, blank=True, null=True)
address_line_2 = models.CharField(max_length=255, blank=True, null=True)
city = models.CharField(max_length=100, blank=True, null=True)
postal_code = models.CharField(max_length=5, blank=True, null=True)
state = models.CharField(max_length=2, blank=True, null=True)
class Meta:
managed = False
db_table = 'buyer_ladder'
unique_together = (('pid', 'did'),)
Я запустил python manage.py makemigrations
без ошибок, и то же самое с python manage.py migrate
. Но когда я попробовал python manage.py migrate --database=paretodb
, я получил django.db.utils.ProgrammingError: (1146, "Table 'paretodb.login_registration_company' doesn't exist")
Почему это ищет таблицу компании в paretodb? Маршрутизатор должен отправлять django на paretodb только для моделей из приложения покупателя, нет?
Кстати, я добавил часть Meta app_label=login_registration
после того, как получил ошибку, надеясь, что это поможет, но это не (та же ошибка)