google-code-export / django-pyodbc

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

ForeignKey not working when specified db_field #3

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. create 2 models, with one-to-many relationship
2. do NOT use default Django field names for PKs, specify them with
db_column paramters in the fields.
3. try to select one object from the "many" side.

What is the expected output? What do you see instead?

Example from the models.py

class Vehicle(models.Model):
    vehicleId = models.IntegerField(db_column='vehicleId', primary_key=True)
...

class Wheel(models.Model):
    wheelId = models.IntegerField(db_column='wheelId', primary_key=True)
    vehicle = models.ForeignKey(Vehicle, db_column='vehicleId',
to_field='vehicleId')
...

In manage.py shell:
from cars.models import *
p = Wheel.objects.all() # this WILL work and return correct number of objects
p1 = p[0] # this will break

What version of the product are you using? On what operating system?
Django trunk version
Python 2.5.2 on Windows XP

Please provide any additional information below.
ipython got this out:
------------- snippet start
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (112, 0))

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (75, 0))

---------------------------------------------------------------------------
ProgrammingError                          Traceback (most recent call last)

C:\...\<ipython console> in <module>()

C:\Python25\lib\site-packages\django\db\models\query.pyc in
__getitem__(self, k)

    229             qs = self._clone()
    230             qs.query.set_limits(k, k + 1)
--> 231             return list(qs)[0]
    232         except self.model.DoesNotExist, e:
    233             raise IndexError, e.args

C:\Python25\lib\site-packages\django\db\models\query.pyc in __len__(self)
    153                 self._result_cache = list(self.iterator())
    154         elif self._iter:
--> 155             self._result_cache.extend(list(self._iter))
    156         return len(self._result_cache)
    157

C:\Python25\lib\site-packages\django\db\models\query.pyc in iterator(self)
    266         extra_select = self.query.extra_select.keys()
    267         index_start = len(extra_select)
--> 268         for row in self.query.results_iter():
    269             if fill_cache:
    270                 obj, _ = get_cached_row(self.model, row, index_start,

C:\Python25\lib\site-packages\django\db\models\sql\query.pyc in
results_iter(sel
f)
    202             else:
    203                 fields = self.model._meta.fields
--> 204         for rows in self.execute_sql(MULTI):
    205             for row in rows:
    206                 if resolve_columns:

C:\Python25\lib\site-packages\django\db\models\sql\query.pyc in
execute_sql(self
, result_type)
   1606
   1607         cursor = self.connection.cursor()
-> 1608         cursor.execute(sql, params)
   1609
   1610         if not result_type:

C:\Python25\lib\site-packages\django\db\backends\util.pyc in execute(self, sql,
params)
     16         start = time()
     17         try:
---> 18             return self.cursor.execute(sql, params)
     19         finally:
     20             stop = time()

C:\Python25\lib\site-packages\django\db\backends\mssql\base.pyc in
execute(self,
 sql, params)

ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server
Driver][SQL Server]Line 1: Incorrect syntax near ')'. (170) (SQLExecDirectW)")

Original issue reported on code.google.com by racami...@gmail.com on 16 Jul 2008 at 11:06

GoogleCodeExporter commented 9 years ago
Sorry, forgot to mention:

MS SQL 2000 database.

Original comment by racami...@gmail.com on 16 Jul 2008 at 11:11

GoogleCodeExporter commented 9 years ago
Same issue as issue 2, fiexd issue 2 also fixed this.

Original comment by vcc.ch...@gmail.com on 18 Jul 2008 at 2:57