Django 2.0.9 using dockerized MS Sql Server on Mac, varbinary conversion error

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am assuming this is NOT a common scenario -- however I am running Django 2.0.9, pyodbc 4.0.24, django-pyodbc-azure 2.0.4.1 and ODBC 17 on my Mac, talking to SQL Server running in Docker, Microsoft SQL Server 2017.

For reasons also beyond my control, I am modeling a system where uploads are stored a BLOBs, varbinary types in SQL Server parlance.

No matter what I do, as soon as I declare a BLOB type and attempt to load a fixture (without a BLOB) I get the error:

Could not load mytype.MyType(pk=3455): ('22018', '[22018] [FreeTDS][SQL Server]Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query. (257) (SQLExecDirectW)')

The model is as follows:

class MyType(models.Model):

    id = models.BigAutoField(primary_key=True)
    desc = models.CharField(
        max_length=4000, blank=True, null=True)
    name = models.CharField(max_length=8, blank=True, null=True)
    blah_id = models.BigIntegerField(blank=True, null=True)
    related_thing = models.ForeignKey(
        Thing,
        on_delete=models.PROTECT,
        blank=True,
        null=True)
    ... etc ...

    pdf = models.BinaryField(blank=True, null=True) # here is the sticking point

    class Meta:
        # etc

I loaddata using a fixture with everything BUT the syllabus and I always get there error:

Problem installing fixture '/path/to/fixtures/0011_mydata.json': Could not load mytype.MyType(pk=3455): ('22018', '[22018] [FreeTDS][SQL Server]Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query. (257) (SQLExecDirectW)')

I believe the error is a red herring, the real problem lies in a driver specific to Mac <-> Sql Server in Docker

Any help is appreciated!

profilepic.png
manpreet 2 years ago

 

The problem, in this case is the connection string:

DATABASES = {
# Microsoft SQL Server version
'default': {
    'ENGINE': 'sql_server.pyodbc',
    'NAME': 'foo',
    'USER': 'blah',
    'PASSWORD': 'password',
    'HOST': '0.0.0.0',
    'PORT': '1433',
    'OPTIONS': {
        'driver': 'FreeTDS',
    },
},
}

Should be

DATABASES = {
# Microsoft SQL Server version
'default': {
    'ENGINE': 'sql_server.pyodbc',
    'NAME': 'foo',
    'USER': 'bar',
    'PASSWORD': 'password',
    'HOST': '0.0.0.0',
    'PORT': '1433',
    'OPTIONS': {
        'driver': 'ODBC Driver 17 for SQL Server',
    },
},
}

FreeTDS was the problem. I do not know why, but eliminating it from the chain fixed the problem.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.