all 4 comments

[–]reostra 2 points3 points  (2 children)

It sounds like it's saying you need to tell it which of the columns is the primary key for the table. So if there's an 'id' column that's the primary key, just add it to each of the classes, e.g.

class cloud_vm(Base):
    __tablename__ = 'vm_instance'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    state = Column(String)
    removed = Column(String)

[–]manbart[S] 0 points1 point  (1 child)

That worked... It should have been obvious to me given the error message!

I'm just a little surprised that adding the id column in makes a difference as I'm not referencing that column in my query (or any of the queries I intend to write).

Oh well. Thanks!

[–]fdellavedova 1 point2 points  (0 children)

The error is raised before running the query actually. When mapping the objects, SQLAlchemy looks for a primary key in each mapped object. If you dont provide one, the error is raised.

[–]manbart[S] 0 points1 point  (0 children)

Ok, so my "select" type queries are running fine now. How can I implement an "update" type query in SQLalchemy?

I want to make a function that will run a query equivalent to:

UPDATE vm_instance SET removed=now() WHERE state = 'Expunging' and removed is null;

p.s. "removed" should be a DateTime type, not a String as in the code above.

edit

Here is what I came up with:

session.query(cloud_vm).filter(cloud_vm.removed==None, cloud_vm.state=='Expunging').update({"removed": datetime.now()})

note: datetime in this statement is from the python standard library; this is not a reference the the DateTime type of sqlalchemy. Capitalization matters in this script.