Skip to content

Raise exception if as_dict=True and column(s) have no names #160

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

Merged
merged 2 commits into from
Jan 9, 2014

Conversation

msabramo
Copy link
Contributor

@msabramo msabramo commented Jan 9, 2014

This is an alternative solution to the issue in #157, suggested by @rsyring.

@rsyring proposed that we modify pymssql to raise an exception when the user uses as_dict=True and then does a query which returns output columns that are not named; the current behavior of omitting those columns is very confusing -- e.g.:

SELECT MAX(x)
FROM (VALUES (1), (2), (3)) AS foo(x)

Note that we don't provide a name for the MAX(x) column above.

cursor = conn.cursor(as_dict=True)
cursor.execute("SELECT MAX(x) FROM (VALUES (1), (2), (3)) AS foo(x)")
print(cursor.fetchall())
# Outputs:
# [{}]
# Whoa, what happened to MAX(x)?!?!

Currently, we don't add the column to the result row if it has no name. This confuses people. See this mailing list thread for an example.

I have a more detailed example at: https://gist.github.com/msabramo/8319097

So the new behavior with this change is:

>>> cursor.execute("SELECT MAX(x) FROM (VALUES (1), (2), (3)) AS foo(x)")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pymssql.pyx", line 426, in pymssql.Cursor.execute (pymssql.c:5828)
    raise ColumnsWithoutNamesError(columns_without_names)
pymssql.ColumnsWithoutNamesError: Specified as_dict=True and there are columns with no names: [0]

and it works nicely even when there are multiple columns without names:

>>> cursor = conn.cursor(as_dict=True)
>>> cursor.execute("SELECT MAX(x), MIN(x) AS [MIN(x)], AVG(x) FROM (VALUES (1), (2), (3)) AS foo(x)")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pymssql.pyx", line 426, in pymssql.Cursor.execute (pymssql.c:5828)
    raise ColumnsWithoutNamesError(columns_without_names)
pymssql.ColumnsWithoutNamesError: Specified as_dict=True and there are columns with no names: [0, 2]

@msabramo
Copy link
Contributor Author

msabramo commented Jan 9, 2014

Cc: @rsyring, @ramiro, @damoxc

Let me know what you think.

@damoxc
Copy link
Contributor

damoxc commented Jan 9, 2014

This seems sane to me, I can't really think of a situation where you would be using as_dict=True and have unnamed columns, I imagine it would just result in a KeyError down the line when they attempt to access the value.

@rsyring
Copy link
Contributor

rsyring commented Jan 9, 2014

Yep, I like this route too.

msabramo added a commit that referenced this pull request Jan 9, 2014
…eption2

Raise exception if as_dict=True and column(s) have no names
@msabramo msabramo merged commit 495a174 into master Jan 9, 2014
@msabramo msabramo deleted the as_dict_No_column_name_raise_exception2 branch January 9, 2014 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants