At the moment, it makes no difference whether I put:
for c in session.query(Church).outerjoin(Minister):
print "<b>",c.name, "</b><br />"
for p in c.people:
print p.surname, "<br />"
or
for c in session.query(Church).outerjoin(Person):
print "<b>",c.name, "</b><br />"
for p in c.people:
print p.surname, "<br />"
What do I need to do to restrict Person shown to Minister?
I had imagined in my naivete that merely creating the subclass would
do it :)
On Nov 28, 9:59 pm, "[email protected]" <[email protected]> wrote:
> OK, I have now gotten rid of the
> AssertionError: No such polymorphic_identity u'Minister' is defined,
> thanks to Kapil. Once I changed minister to Minister, all was OK.
> Thanks, Kapil!
>
> However, I have a supplementary question.
> Whilst I can put .filter(Person.role_id==1) to restrict people shown
> to Ministers,
> currently, whether I use Minister or Person in the query does not seem
> to matter.
> How do I bind Ministers to the Minister(Person) class?
>
> On Nov 28, 9:21 pm, Kapil Thangavleu <[email protected]> wrote:
>
>
>
> > minister vs Minister
>
> > -k
>
> > Excerpts from [email protected]'s message of Mon Nov 28 15:16:38 -0500 2011:
>
> > > '''
> > > vacant.py contains the following:
> > > '''
>
> > > #!/usr/bin/env python
> > > #This script ran correctly on 2011-11-17
>
> > > import cgi
> > > import cgitb; cgitb.enable() # for troubleshooting
>
> > > print "Content-type: text/html"
> > > print
> > > #print "This is working now."
>
> > > import csv
> > > reader = csv.reader(open("/home/calum/Desktop/list3.csv", "rb"))
>
> > > from itertools import *
> > > from jinja2 import Template
>
> > > from sqlalchemy import *
> > > from sqlalchemy.ext.declarative import declarative_base
> > > from sqlalchemy.orm import sessionmaker, relationship, mapper, backref
> > > from sqlalchemy import and_, not_
>
> > > engine = create_engine('sqlite:///vacant.db', echo=False)
> > > Base = declarative_base(bind=engine)
> > > Session = (sessionmaker(engine))
>
> > > charges = {} # charges
> > > churches = {} # churches
> > > people = {} # people
> > > roles = {} # roles
> > > seats = {} # categories
>
> > > class Person(Base):
> > > __tablename__ = 'people'
> > > id = Column(Integer, primary_key=True)
> > > title = Column(String)
> > > forenames = Column(String)
> > > surname = Column(String)
> > > degrees = Column(String)
> > > seat = Column (String)
> > > discriminator = Column ('type', String)
> > > __mapper_args__ = {'polymorphic_on': discriminator}
>
> > > role_id = Column(Integer, ForeignKey('roles.id'))
> > > role = relationship ("Role", backref = backref('people'))
>
> > > church_id = Column(Integer, ForeignKey('churches.id'))
> > > church = relationship ("Church", backref = backref('people'))
>
> > > seat_id = Column(Integer, ForeignKey('seats.id'))
> > > seat = relationship ("Seat", backref = backref('people'))
>
> > > class Minister(Person):
> > > __mapper_args__ = {'polymorphic_identity': 'minister'}
>
> > > class Seat(Base):
> > > __tablename__ = 'seats'
> > > id = Column(Integer, primary_key=True)
> > > name = Column(String)
>
> > > class Role(Base):
> > > __tablename__ = 'roles'
> > > id = Column(Integer, primary_key=True)
> > > name = Column(String)
>
> > > class Charge(Base):
> > > __tablename__ = 'charges'
> > > id = Column(Integer, primary_key=True)
> > > name = Column(String)
>
> > > class Church(Base):
> > > __tablename__ = 'churches'
> > > id = Column(Integer, primary_key=True)
> > > name = Column(String)
> > > charge_id = Column(Integer, ForeignKey('charges.id'))
> > > charge = relationship ("Charge", backref = backref('churches'))
>
> > > Base.metadata.create_all()
> > > session = Session()
>
> > > for row in reader:
> > > title =''
> > > # isolate surname
> > > surname = row[0].strip()
>
> > > # isolate forenames
> > > forenames = row[1].strip()
>
> > > # isolate church
> > > church = row[3].strip()
> > > if church not in churches:
> > > churches[church] = Church(name=church)
>
> > > # isolate charge
> > > charge = row[2].strip()
> > > if charge not in charges:
> > > charges[charge] = Charge(name = charge)
>
> > > # isolate role
> > > core = row[4].strip()
> > > discriminator = core
> > > seat = core
> > > if seat not in seats:
> > > seats[seat] = Seat(name = seat)
>
> > > role = row[4].strip()
> > > if role not in roles:
> > > roles[role] = Role(name = role)
>
> > > # isolate person
> > > person = row[5]
> > > if len(person.split(' '))>1:
> > > (fullname, degrees) = person.split(' ')
> > > deg = degrees.split(' ')
> > > degrees = ', '.join(deg)
> > > else:
> > > degrees = ''
> > > fullname = person
>
> > > ainm = fullname.split(' ')
>
> > > t = ainm[0]
> > > title = ''.join(t)
> > > ainm.remove(t)
>
> > > if ainm:
> > > t = ainm[0]
> > > if t == "Dr" or t == "Revd":
> > > title = title +' '+ t
> > > ainm.remove(t)
>
> > > if ainm:
> > > t = ainm[0]
> > > if t == "Dr":
> > > title = title +' '+ t
>
> > > name = title +' '+ forenames +' '+ surname +' '+ degrees
>
> > > people[name] = Person(title=title, forenames=forenames,
> > > surname=surname, degrees=degrees, discriminator=discriminator)
> > > people[name].church = churches[church]
> > > people[name].role = roles[role]
> > > people[name].seat = seats[seat]
> > > people[name].discriminator = discriminator
> > > churches[church].charge = charges[charge]
>
> > > session.add_all(churches.values())
> > > session.add_all(charges.values())
> > > session.add_all(roles.values())
> > > session.add_all(seats.values())
> > > session.add_all(people.values())
>
> > > session.commit()
>
> > > print "Database created and populated!<br />"
> > > #[Sunday 27 November 2011] [16:12:33] <virhilo> pangur:
> > > DBSession.query(Churh).filter(sqlalchemy.not_(Church.id.in_(DBSssion.query(Minister.church_id).subquery()))
>
> > > for c in session.query(Church).outerjoin(Person):
> > > print "<b>",c.name, "</b><br />"
> > > for p in c.people:
> > > print p.surname, "<br />"
>
> > > ######################
> > > '''
> > > This produces the traceback:
>
> > > Database created and populated!
> > > Minister without charge
> > > --> -->
> > > Traceback (most recent call last):
> > > File "/cserver/cgi/vacant.py", line 160, in <module>
> > > for p in c.people:
> > > File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.2-
> > > py2.7.egg/sqlalchemy/orm/attributes.py", line 168, in __get__
> > > return self.impl.get(instance_state(instance),dict_)
> > > File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.2-
> > > py2.7.egg/sqlalchemy/orm/attributes.py", line 420, in get
> > > value = self.callable_(state, passive)
> > > File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.2-
> > > py2.7.egg/sqlalchemy/orm/strategies.py", line 563, in _load_for_state
> > > result = q.all()
> > > File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.2-
> > > py2.7.egg/sqlalchemy/orm/query.py", line 1729, in all
> > > return list(self)
> > > File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.2-
> > > py2.7.egg/sqlalchemy/orm/query.py", line 1960, in instances
> > > rows = [process[0](row, None) for row in fetch]
> > > File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.2-
> > > py2.7.egg/sqlalchemy/orm/mapper.py", line 2494, in _instance
> > > _instance = polymorphic_instances[discriminator]
> > > File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.2-
> > > py2.7.egg/sqlalchemy/util/_collections.py", line 569, in __missing__
> > > self[key] = val = self.creator(key)
> > > File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.2-
> > > py2.7.egg/sqlalchemy/orm/mapper.py", line 2681, in
> > > configure_subclass_mapper
> > > discriminator)
> > > AssertionError: No such polymorphic_identity u'Minister' is defined
> > > '''
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.