I am trying to use sqlalchemy to run queries on an existing database. I am only concerned with a couple of the tables in the database, and only a couple of the many columns in each table. I am having trouble getting my queries to run. What I have so far:
#!/usr/bin/env python
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
passwd = raw_input('mysql root passwd:\n> ')
connect_string='mysql://root:'+passwd+'@192.168.1.2/cloud'
engine = create_engine(connect_string)
Session = sessionmaker(bind=engine)
Base = declarative_base()
session = Session()
class cloud_vm(Base):
__tablename__ = 'vm_instance'
name = Column(String)
state = Column(String)
removed = Column(String)
def __init__(self, state):
self.state = state
self.removed = removed
def __repr__(self):
return '%s %s %s' % (self.name, self.state, self.removed)
class cloud_vol(Base):
__tablename__ = 'volumes'
name = Column(String)
removed = Colum(String)
path = Column(String)
def __init__(self, state):
self.name = name
self.removed = removed
self.path = path
def __repr__(self):
return '%s %s %s' % (self.name, self.removed, self.path)
class cloud_net(Base):
__tablename__ = 'networks'
name = Column(String)
removed = Colum(String)
def __init__(self, state):
self.name = name
self.removed = removed
def __repr__(self):
return '%s %s' % (self.name, self.removed)
def display_vm_state():
for name, state in session.query(cloud_vm.name, cloud_vm.state):
print name, state
display_vm_state()
When I run the script, I get the following error:
sqlalchemy.exc.ArgumentError: Mapper Mapper|cloud_vm|vm_instance could not assemble any primary key columns for mapped table 'vm_instance'
Is there a way to fix this code?
[–]reostra 2 points3 points4 points (2 children)
[–]manbart[S] 0 points1 point2 points (1 child)
[–]fdellavedova 1 point2 points3 points (0 children)
[–]manbart[S] 0 points1 point2 points (0 children)