Sqlalchemy from_statement() cannot locate column

2024/10/9 16:28:34

I am following the sqlalchemy tutorial in http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html

Nevertheless, instead of using a SQLite backend, I am using MySQL. The problem is that when I try to execute a literal MySQL statement to select a column from the users table, such as

SELECT name from users;

it will fail with

NoSuchColumnError: "Could not locate column in row for column 'users.id'"

whereas doing a

SELECT * FROM users

will work just fine. Am I missing anything?

The Users class is defined as :

class User(Base):__tablename__ = 'users'id = Column(Integer, primary_key=True)name = Column(String(50))fullname = Column(String(50))password = Column(String(50))def __repr__(self):return "<User(name='%s', fullname='%s', password='%s')>" % (self.name, self.fullname, self.password)

The session is defined as:

Session = sessionmaker(bind=engine)
session = Session()

And I am trying to do

session.query(Users).from_statement('SELECT name from users')

By the way, this statement works fine when run on a MySQL client. Am I missing something or is it a bug?

Answer

I answer myself in case someone has the same problem. The syntax of

session.query(Users).from_statement('SELECT name from users')

is wrong. It should be

session.query('name').from_statement('SELECT name from users')

The list of columns should be in the call to query().

https://en.xdnf.cn/q/69997.html

Related Q&A

Python - how to check if weak reference is still available

I am passing some weakrefs from Python into C++ class, but C++ destructors are actively trying to access the ref when the real object is already dead, obviously it crashes...Is there any Python C/API a…

Django using locals() [duplicate]

This question already has answers here:Django template and the locals trick(8 answers)Closed 5 years ago.I am beginner in web development with Django. I have noticed that the locals() function is used …

python ghostscript: RuntimeError: Can not find Ghostscript library (libgs)

When trying to run hello-world exampleimport sys import ghostscriptargs = ["ps2pdf", # actual value doesnt matter"-dNOPAUSE", "-dBATCH", "-dSAFER","-sDEVICE…

what is the default encoding when python Requests post data is string type?

with fhe following codepayload = 工作报告 总体情况:良好 r = requests.post("http://httpbin.org/post", data=payload)what is the default encoding when Requests post data is string type? UTF8…

How to initialize a database connection only once and reuse it in run-time in python?

I am currently working on a huge project, which constantly executes queries. My problem is, that my old code always created a new database connection and cursor, which decreased the speed immensivly. S…

Django - ModelForm: Add a field not belonging to the model

Note: Using django-crispy-forms library for my form. If you have a solution to my problem that involves not using the cripsy_forms library, I accept it all the same. Not trying to be picky just need a …

Row by row processing of a Dask DataFrame

I need to process a large file and to change some values.I would like to do something like that:for index, row in dataFrame.iterrows():foo = doSomeStuffWith(row)lol = doOtherStuffWith(row)dataFrame[col…

Tweepy Why did I receive AttributeError for search [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Running qsub with anaconda environment

I have a program that usually runs inside a conda environmet in Linux, because I use it to manage my libraries, with this instructions:source activate my_environment python hello_world.pyHow can I run …

Flask Application was not able to create a URL adapter for request [duplicate]

This question already has answers here:Flask.url_for() error: Attempted to generate a URL without the application context being pushed(3 answers)Closed 10 months ago.I have this code which used to work…