WilliamRen / django-pyodbc

Automatically exported from code.google.com/p/django-pyodbc
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Aliases not created even though MS SQL Server is not case sensitive #111

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
This is largely for legacy databases, where column names were not capitalized 
consistently.

class Parent(models.Model):
    title = models.CharField(max_length=100, db_column="Title")

class Child(models.Model):
    parent = models.ForeignKey(Parent, related_name="children")
    title = models.CharField(max_length=100, db_column="title")

Child.objects.select_related("parent__pk").all()

This will generally result in 

The column 'Title' was specified multiple times for 'X'

The problem is: according to Python, "Title" and "title" are distinct. So an 
alias isn't being created, because it isn't needed. According to MS SQL, 
however, they're the same.

The non-programmatic fix is to change the column names in the database so that 
they share the same case, and have that reflect as well in the models:

class Parent(models.Model):
    title = models.CharField(max_length=100, db_column="title")

class Child(models.Model):
    parent = models.ForeignKey(Parent, related_name="children")
    title = models.CharField(max_length=100, db_column="title")

However, a better fix would be for django-pyodbc to be adjusted to account for 
the fact that MSSQL isn't case sensitive. Fields that match on a 
case-insensitive level should become aliases.

Original issue reported on code.google.com by jordanth...@gmail.com on 2 Sep 2011 at 6:12

GoogleCodeExporter commented 8 years ago
I can also confirm that this is an issue.

Original comment by djfis...@gmail.com on 3 Oct 2012 at 12:05