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?
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With