Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simply sqlalchemy filter not working

Select all works like this:

q = session.query(products)

Now I want to add a WHERE filter, so I am trying:

q = session.query(products).filter_by(stock_count=0)

I get an error saying 'nonetype' object has no attribute 'class_manager'.

Not sure what the issue is?

Update The column seems to be mapped fine, as when I do:

q = session.query(products)

for p in q:
   print p.stock_count

It outputs the value.

But if I do:

p.stock_count = 6

I get an error also, saying: "can't set attribute"

So I can query for it, but adding the column as a filter, OR setting the value causes an error.

Strange no?

like image 202
Blankman Avatar asked Dec 14 '25 20:12

Blankman


1 Answers

You may be trying to use the orm against a bare Table object.

This code works on 0.5 (the one in base centos 6.2):

#!/usr/bin/env python
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

db = create_engine(localopts.connect_string)
Session = sessionmaker(bind=db)

Base = declarative_base()
Base.metadata.reflect(bind=db)

class some_table(Base): 
    __table__ = Base.metadata.tables['table_name']

session = Session()

for row in session.query(some_table.username).filter_by(username="some-user"):
    print row
like image 191
mikefedyk Avatar answered Dec 17 '25 11:12

mikefedyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!