Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'InsertQuery' object has no attribute 'columns' #30

Open
rbalch opened this issue Dec 3, 2013 · 4 comments
Open

'InsertQuery' object has no attribute 'columns' #30

rbalch opened this issue Dec 3, 2013 · 4 comments

Comments

@rbalch
Copy link

rbalch commented Dec 3, 2013

I am having a problem getting this project to run in various setups. These are all Linux and/or osX using FreeTDS.

I have an old version of one of these projects that a few years ago I customized to get working in these environments. I have lots of schema's in a pre-existing MsSQL box that I mapped to django ORM objects. The default database is postgres, but I have a particular app and all the models in it mapped with the multi-database support.

I'm running Django 1.5.5, pyodbc 3.0.6, and now django-pyodbc 0.10. I can run select style queries just fine like:

print Example.objects.all().count()

... anything that generates a select-style query seems ok. But anything like:

s = Example()
s.save()

results in:

Traceback (most recent call last):
File "/Users/ryan/Google Drive/code/cortex/temp.py", line 107, in
rss.save()
File "/Users/ryan/Google Drive/code/cortex/axons/models.py", line 62, in save
super(DateModel, self).save(_args, *_kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 546, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 650, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 215, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 1675, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 942, in execute_sql
for sql, params in self.as_sql():
File "/usr/local/lib/python2.7/site-packages/django_pyodbc/compiler.py", line 283, in as_sql
result.append('(%s)' % ', '.join([qn(c) for c in self.query.columns]))
AttributeError: 'InsertQuery' object has no attribute 'columns'

@ghost ghost assigned dlo Dec 3, 2013
@julioalpa
Copy link

Hi, i have the same problem.

Could you resolve this issue?
How??

Best Regards,
Julio Sánchez.

@rbalch
Copy link
Author

rbalch commented Nov 21, 2014

I did finally resolve it, but after a year or so I'm having trouble remembering. Over the last few years I've actually made a few little patches to pyodbc that made it work better with django. Looking through git logs I think I made a simple patch to the pyodbc/compiler.py file.

In the file there's a function "as_sql", and in there I swapped out:

    if self.return_id and self.connection.features.can_return_id_from_insert:
        params = params[0]
        output = 'OUTPUT inserted.%s' % qn(opts.pk.column)
        result.append(output)
        result.append("VALUES (%s)" % ", ".join(placeholders[0]))
        return [(" ".join(result), tuple(params))]

with:

    if self.return_id and self.connection.features.can_return_id_from_insert:
        params = params[0]
        #TODO: ryan commented out old output method and replace with scope_identity
        # output = 'OUTPUT inserted.%s' % qn(opts.pk.column)
        # result.append(output)
        result.append("VALUES (%s)" % ", ".join(placeholders[0]))
        result.append("SELECT SCOPE_IDENTITY()")
        return [(" ".join(result), tuple(params))]

... let me know if that doesn't work and i'll dig into it a bit more

@julioalpa
Copy link

Thanks eviltandem for your support!!

mmmm but this code is just under where i get the error code..

result.append('INSERT INTO %s' % qn(opts.db_table))
result.append('(%s)' % ', '.join([qn(c) for c in self.query.columns])) # <=== here is the error

if self.return_id and self.connection.features.can_return_id_from_insert:
params = params[0]
#TODO: ryan commented out old output method and replace with scope_identity
# output = 'OUTPUT inserted.%s' % qn(opts.pk.column)
# result.append(output)
result.append("VALUES (%s)" % ", ".join(placeholders[0]))
result.append("SELECT SCOPE_IDENTITY()")
return [(" ".join(result), tuple(params))]

Can you give me the columns part? Or maybe all the compile.py

BR,
Julio Sánchez.

@julioalpa
Copy link

Done!, I was replace all the as_sql code to the as_code from Github lastest version.

It Works!!

Thanks a lot eviltandem!!

Best Regards,
Julio Sánchez.

@dlo dlo removed their assignment Aug 11, 2015
@dlo dlo added the help wanted label Feb 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants