Hi This seems like a genuine bug, Django should not assume that all backends have the same max table name length. Please file a ticket.
You can workaround this right now by defining db_table on your model. You might need two model classes, sharing details via inheritance, for the different backends though. I can see two solutions, either: - truncate to the minimum max length across all models - remove the truncation behaviour and instead have a system check error triggered when a table name is too long for at least one backend. This would allow users to be explicit about the fix, would not change table names when adding/changing database backends, and would remove the somewhat undesirable/confusing hash at the end of table names. Either looks like they'd need to go through the deprecation pathway. Thanks, Adam On Wed, 14 Apr 2021 at 11:53, Javier Buzzi <buzzi.jav...@gmail.com> wrote: > 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 > <https://groups.google.com/d/msgid/django-developers/95d2dd47-a635-4c6d-86ce-8dde8e37e536n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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/CAMyDDM2X90qxkYQ3Jw701%3DwfSw%2Bv2-qEpXsEw5Mdbvacf54Rgg%40mail.gmail.com.