[sqlalchemy] How to fix mapped entity is required error?

2012-04-03 Thread Oltmans
Greetings, I'm using SQLAlchemy 0.6 with Python 2.6

medical-app/

facility/
recalls.py
common/
__init__.py
patient.py
common.py
settings.py

So the application I'm working on has directory structure as above.
Program starts when
I run recalls.py (which is under /medical-app/facility). Also,
recalls.py imports common.py
(which is under medical-app/common/common.py). Common.py further
imports settings.py which
has code like following

USERNAME = 'root'
PASSWORD = ''
SERVER = 'localhost'
DB_NAME = localdb'
DB = create_engine('mysql://'+USERNAME+':'+PASSWORD+'@'+SERVER
+'/'+DB_NAME, pool_size = 20, pool_recycle=100)
db_session = session.sessionmaker(bind=DB, expire_on_commit=False)
alchemy_session = db_session()
META = MetaData(DB)

In patient.py, I've declarative style classes as below


from common.settings import alchemy_session, DB, META
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Table

Base = declarative_base()

class Patient(Base):
__table__ = Table('Patient', Base.metadata,
autoload=True, autoload_with=DB)


Then finally in recalls I do the following

from common import alchemy_session
from common.patient import Patient

patient = alchemy_session.query(Patient).filter(Patient.patientId ==
1).first()#this works
patient = alchemy_session.query(Patient).filter(Patient.patientId ==
2).first()#this fails
#with following error




patient = alchemy_session.query(Patient).filter(
  File c:\python26\lib\site-packages\sqlalchemy-0.7.1-py2.6.egg
\sqlalchemy\orm\
session.py, line 897, in query
return self._query_cls(entities, self, **kwargs)
  File c:\python26\lib\site-packages\sqlalchemy-0.7.1-py2.6.egg
\sqlalchemy\orm\
query.py, line 105, in __init__
self._set_entities(entities)
  File c:\python26\lib\site-packages\sqlalchemy-0.7.1-py2.6.egg
\sqlalchemy\orm\
query.py, line 112, in _set_entities
entity_wrapper(self, ent)
  File c:\python26\lib\site-packages\sqlalchemy-0.7.1-py2.6.egg
\sqlalchemy\orm\
query.py, line 2800, in __init__
expected - got '%r' % column
sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped
entity exp
ected - got 'class 'vi_pycommon.patient.Patient''

D:\integration-scripts
\allendale_bone_jointalchemy_session.query(Patient).filte
r(Patient.patientId == 2).first()
---

so if you notice this line didn't fail
patient = alchemy_session.query(Patient).filter(Patient.patientId ==
1).first()

but next line did, which was
patient = alchemy_session.query(Patient).filter(Patient.patientId ==
2).first()#this fails

So I'm actually really stumped as to why this second call is failing
with that error. I am absolutely
sure I'm doing something wrong here, so kindly let me know how can I
fix this one.

Thanks in advance for all help.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] How to fix mapped entity is required error?

2012-04-03 Thread Michael Bayer

On Apr 3, 2012, at 11:05 AM, Oltmans wrote:

 Greetings, I'm using SQLAlchemy 0.6 with Python 2.6
 
 from common import alchemy_session
 from common.patient import Patient
 
 patient = alchemy_session.query(Patient).filter(Patient.patientId ==
 1).first()#this works
 patient = alchemy_session.query(Patient).filter(Patient.patientId ==
 2).first()#this fails
 #with following error
 
 sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped
 entity exp
 ected - got 'class 'vi_pycommon.patient.Patient''
 
 D:\integration-scripts
 \allendale_bone_jointalchemy_session.query(Patient).filte
 r(Patient.patientId == 2).first()
 ---
 
 so if you notice this line didn't fail
 patient = alchemy_session.query(Patient).filter(Patient.patientId ==
 1).first()
 
 but next line did, which was
 patient = alchemy_session.query(Patient).filter(Patient.patientId ==
 2).first()#this fails
 
 So I'm actually really stumped as to why this second call is failing
 with that error. I am absolutely
 sure I'm doing something wrong here, so kindly let me know how can I
 fix this one

Looks like SQLA 0.7.1 from the stack trace.  Anyway the error indicates 
Patient is not mapped so this would suggest clear_mappers() is being called 
somewhere, as there aren't many paths to un-map a class besides that.  Or the 
identifier Patient is somehow changing.

The key here is to begin shrinking the program into a single-file test case 
that you can send here, and in the process of getting there you'll probably 
identify the single thing that makes the behavior change.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.