Skip to content

A replase wont work when DB name exceeds 30 characters #423

Description

Software versions

  • Django: 4.2.0
  • mssql-django: 1.5
  • python: 3.11
  • SQL Server: 18
  • OS: debian

Problem description and steps to reproduce
precondition
A db name should be more than 30 characters lenght, e.g. aaa0123456789012345678901234567890123456789

code
when I'd run the code. below

MyObject.objects.filter(some_field="some value")
.update(some_field=Replace("some_field", Value('value'), Value('data')))

When run the code above I've got exception like this one #420 (comment)

the issue with the DB name and sqlserver_replace
the SQL QUERY
SELECT CONVERT(varchar, DATABASEPROPERTYEX(CONVERT(varchar, (SELECT DB_NAME())), 'collation'))
is truncate DB name to 30
e.g.

select 
      CONVERT(varchar, 'aaa0123456789012345678901234567890123456789') as a, 
                   len('aaa0123456789012345678901234567890123456789') as b, 
  len(CONVERT(varchar, 'aaa0123456789012345678901234567890123456789')) as c

will return

a b c
aaa012345678901234567890123456 43 30

Expected behavior and actual behavior
Need to change to SELECT CONVERT(varchar(max), DATABASEPROPERTYEX(CONVERT(varchar(max), (SELECT DB_NAME())), 'collation'))

Error message/stack trace

  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1982, in execute_sql
    cursor = super().execute_sql(result_type)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1547, in execute_sql
    sql, params = self.as_sql()
                  ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/mssql/compiler.py", line 666, in as_sql
    sql, params = super().as_sql()
                  ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1954, in as_sql
    sql, params = self.compile(val)
                  ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/mssql/compiler.py", line 442, in compile
    return super().compile(node, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 542, in compile
    sql, params = vendor_impl(self, self.connection)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/mssql/functions.py", line 65, in sqlserver_replace
    current_collation = default_collation.replace('_CI', '_CS')
                        ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'replace'

Any other details that can be helpful
A db name should be more than 30 characters lenght, e.g. aaa0123456789012345678901234567890123456789

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: data-types-dmlType mapping (UUID, varchar/nvarchar, Unicode, decimal), bulk DML, param limitsbugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions