Форум сайта python.su
Имеется система:
>>> sys.version
'2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)]'
>>> sqlalchemy.__version__
'0.5.0rc4'
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData
from sqlalchemy.orm import mapper, sessionmaker
db_name = 'xxx'
db_login = 'xxx'
db_password='xxx'
class Group(object):
"Class for table develop.notify_groups"
def __init__(self, id, name):
self.id = id
self.name = name
def __repr__(self):
return ("<Group({0},'{1}')>").format(self.id, self.name)
engine = create_engine('oracle://{0}:{1}@{2}'.format(db_login,db_password,db_name),use_ansi=False)
metadata = MetaData()
group_table = Table('notify_groups', metadata,
Column('id', Integer, primary_key=True),
Column('name', String))
mapper(Group, group_table)
session = sessionmaker(bind=engine)()
print(session.query(Group))
for group in session.query(Group):
print(group)
SELECT notify_groups.id AS notify_groups_id, notify_groups.name AS notify_groups_name
FROM notify_groups
<Group(1,'Administrators')>
<Group(2,'Billing')>
<Group(3,'Monitoring')>
<Group(4,'Fraudmanagers')>
<Group(5,'Developers')>
--- group_table = Table('notify_groups', metadata,
+++ group_table = Table('develop.notify_groups', metadata,
SELECT "develop.notify_groups".id AS "develop.notify_groups_id", "develop.notify_groups".name AS "develop.notify_groups_name"
FROM "develop.notify_groups"
Traceback (most recent call last):
File "C:\Documents and Settings\okovalenko\workspace\oracle_test\src\test_sqlalch.py", line 30, in <module>
for group in session.query(Group):
File "c:\python26\lib\site-packages\SQLAlchemy-0.5.0rc4-py2.6.egg\sqlalchemy\orm\query.py", line 1088, in __iter__
return self._execute_and_instances(context)
File "c:\python26\lib\site-packages\SQLAlchemy-0.5.0rc4-py2.6.egg\sqlalchemy\orm\query.py", line 1091, in _execute_and_instances
result = self.session.execute(querycontext.statement, params=self._params, mapper=self._mapper_zero_or_none(), _state=self._refresh_state)
File "c:\python26\lib\site-packages\SQLAlchemy-0.5.0rc4-py2.6.egg\sqlalchemy\orm\session.py", line 750, in execute
clause, params or {})
File "c:\python26\lib\site-packages\SQLAlchemy-0.5.0rc4-py2.6.egg\sqlalchemy\engine\base.py", line 806, in execute
return Connection.executors[c](self, object, multiparams, params)
File "c:\python26\lib\site-packages\SQLAlchemy-0.5.0rc4-py2.6.egg\sqlalchemy\engine\base.py", line 856, in execute_clauseelement
return self.__execute_context(context)
File "c:\python26\lib\site-packages\SQLAlchemy-0.5.0rc4-py2.6.egg\sqlalchemy\engine\base.py", line 878, in __execute_context
self._cursor_execute(context.cursor, context.statement, context.parameters[0], context=context)
File "c:\python26\lib\site-packages\SQLAlchemy-0.5.0rc4-py2.6.egg\sqlalchemy\engine\base.py", line 927, in _cursor_execute
self._handle_dbapi_exception(e, statement, parameters, cursor)
File "c:\python26\lib\site-packages\SQLAlchemy-0.5.0rc4-py2.6.egg\sqlalchemy\engine\base.py", line 909, in _handle_dbapi_exception
raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect)
sqlalchemy.exc.DatabaseError: (DatabaseError) ORA-00942: table or view does not exist
'SELECT "develop.notify_groups".id AS "develop.notify_groups_id", "develop.notify_groups".name AS "develop.notify_groups_name" \nFROM "develop.notify_groups"' {}
Офлайн
Попробуй, как в документации пишут, чем черт не шутит, вруг получится.
..bw
Офлайн
bwСпасибо. Действительно работает. Теперь сгенерированный SQL-зпрос имеет вид:
Попробуй, как в документации пишут, чем черт не шутит, вруг получится.
SELECT develop.notify_groups.id AS develop_notify_groups_id, develop.notify_groups.name AS develop_notify_groups_name
FROM develop.notify_groups
Офлайн