I have a model with a really long name think `ThisIsAReallyLongModelNameThatIsAlsoVeryOld`. To complicate things further I have a Oracle instance without archival data designated connection name `old` and a new postgres instance with the connection name `default`.
Issue i've found is that if i try and call the model `ThisIsAReallyLongModelNameThatIsAlsoVeryOld.objects.using('old').count()` i get an error saying that the table `APP_THISISAREALLYLONGMODEL5300` does not exist, when it should be using `APP_THISISAREALLYLONGMODEL5BD6` instead. By the way when I use postgres, it works as expected: `ThisIsAReallyLongModelNameThatIsAlsoVeryOld.objects.using('default').count()` This is because the default connection is used when the model is instantiated, and then its used from that moment on. https://github.com/django/django/blob/stable/3.2.x/django/db/models/options.py#L207 This is an issue that seems to go back to the beginning of Django. Is this something known? Is this something we can fix? Thanks, ================= here be dragons =================== as a temporary hack i did this for now, sorry for the formatting, google groups didn't let me format it the way i wanted. from django.db.models.sql import compiler from django.db.backends.utils import truncate_name class ConnectionAwareAlias(dict): def __init__(self, query, connection): self.query = query self.connection = connection self['*'] = '*' def __contains__(self, item): model = self.query.model db_table = "%s_%s" % (model._meta.app_label, model._meta.model_name) if item == db_table: if not self.get(item): db_table = truncate_name(db_table, self.connection.ops.max_name_length()) self[item] = db_table return True return self.get(item) is not None class NewSQLCompiler(compiler.SQLCompiler): def __init__(self, query, connection, using): super(NewSQLCompiler, self).__init__(query, connection, using) self.quote_cache = ConnectionAwareAlias(query, connection) compiler.SQLCompiler = NewSQLCompiler -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/95d2dd47-a635-4c6d-86ce-8dde8e37e536n%40googlegroups.com.