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
dict(row) causing TypeError: tuple indices must be integers or slices, not str #6218
Comments
I haven't been able to recreate this either, this looks related to your environment. Maybe there is something in the compile args for Python in the image? Alpine images can often be odd, and are famous for being hard to debug. |
You cannot recreate with the alpine dockerfile? |
@lindycoder Sorry, like you I could not recreate this on any other environment. I tried a few ubuntu and osx variants. I left the |
@jvanasco okay i understand The fact that NOT removing g++ fixes the problem might be an indication of an installation thing. Is SQL Alchemy doing things differently if a compiler is available? |
It may be a bug in the python version of the library. Can you reproduce by installing it on debian (or other) with The reason that on debian and other system install with compiled extension is because we publish wheel with them active |
we should also keep in mind that |
@lindycoder There are some c-extensions.
@CaselIT What about testing on alpine with g++ and disabling cext? |
@zzzeek We started using this a couple weeks ago when we upgraded to a recent version of SQLAlchemy and this stopped working I could work around using row._asdict() which returns _mapping :) If this is deprecated, what is the official way of translating a row to a dict? |
alpine will download the source distribution, then if it finds the compile it will compile the c-extensions, if it does not find the compile no c-extension is compile. the evn variable |
|
@jvanasco @CaselIT Alpine
Debian
|
_asdict() or _mapping https://docs.sqlalchemy.org/en/14/core/connections.html#sqlalchemy.engine.Row._mapping |
@zzzeek we seem to have a bug in the documentation. |
@zzzeek the python implementation should match the c ones in this case right? |
well it's part of namedtuple so...sure it should be a method that's doc'ed |
you mean the behavior? sure |
So I see documentation says I'm okay with closing this issue, maybe that method could spit our warnings? |
|
The issue in in the python code. The c code works with |
oh. OK I saw all the "Cant reproduce" and assumed it was weirder than that. reproduces right away w/ python code!
|
oh ok. OK. sorry. so what's happening here is that Session.execute() is returning the future version of This is a hard one because if the given statement has ORM entities, you get the new style row back, and it's hard for people to tell between:
the idea is that if those two returned different kinds of Row objects that would really confuse people. So maybe the Python version of row should still have a fallback here, seeing that the C one seems to. |
there's no 2.0 deprecation warning either. this is pretty broken |
pretty broken, the C BaseRow uses BaseRow_subscript_mapping as the universal |
Mike Bayer has proposed a fix for this issue in the master branch: Fix LegacyRow/Row index access https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2721 |
alright well i have somethign to work on for 1.4.7 |
Will upgrading to 1.4.7 fix this? I am facing the exact issue today with 1.4.2 version. In AWS environment sqlalchemy deployed in AWS Lambda itself works fine if I unpack the CursorResult like {**row}
But same thing fails if I deploy sqlalchemy 1.4.2(same version) in Layers instead of Lambda directly. Is there any fix for this? |
Have you tried updating sqlalchemy? 1.4.2 is quite old, and early versions of the 1.4 series had a few bugs that were fixed in later releases |
I'll try to update to 1.4.7 and retry, thank you! |
Any reason for not using the last version on the 1.4 sereis? |
For context on @CaselIT’s comment, 1.4.40 is the latest version in the 1.4 series. |
Nothing particularly, will update it, thanks |
Old issue, I apologize, but I just encountered this in a very surprising way wherein I tried to deploy my app to my webserver and it broke in horrible ways due to this error on the 2.0 branch. Reverting to 1.4 fixes this but I'm not sure what the proper fix should be? Is there an example of changes that need to be done between 1.4 and 2.0? |
Maybe _asdict() helps? I'd suggest reading the excellent migration notes, and in particular the Result section. There you can find an alternative to the above. |
DISCLAIMER: This is a very weird bug, I don't know if SQLAlchemy is the problem, but I couldn't reproduce without it, there is a very easy workaround, but I felt like i should report this as it may hide something else.
Describe the bug
Using
dict()
on ansqlalchemy.engine.Row
object is producingTypeError: tuple indices must be integers or slices, not str
in a very specific setup (see To Reproduce)Expected behavior
dict(row)
should return the row as a dict.To Reproduce
This Dockerfile can show the error:
Inline code is
Error
Versions.
Additional context
The provided Dockerfile is a shrunk down version of what we use:
I have noticed that REMOVING or MOVING the
RUN apk del g++
AFTER the installation of SQL Alchemy will solve the problem.I also tried to reproduce in debian, compiling libs instead of wheels. with this Dockerfile and could not reproduce, so maybe it's related to Alpine OR there's something else installed in slim-buster.
WORKAROUND
Using
row._asdict()
will solve the problem.Have a nice day! and thank you for your amazing work.
The text was updated successfully, but these errors were encountered: