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

Course Queries Syllabus Queries 3 years ago

7.36K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 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!

0 views
0 shares

profilepic.png
manpreet 3 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.

Similar Forum