not totally sure what you’re going for here as I think you’re looking at the problem using a different vocabulary. From the query you have at the bottom, it appears you’d want this:
for teacher in session.query(Teacher).join(Teacher.aula).filter(Aula.nr_aula=='B2'): print teacher.id, teacher.name for aula in teacher.aula: print aula.nr_aula overall the best approach is to figure out what SQL represents the query you’re trying to do, then we can express that using a Query. On Nov 25, 2013, at 2:37 PM, mando <mandol...@gmail.com> wrote: > Hi to all, > I've copied ([1]) and rewrote some parts of this code [0] that is usefull for > my project, where I need to reusing some tables as a child of many parents. > In concrete I need something like this: > > Table_parent_1 > > Table_parent_2 > > Table_child_1 > > Table_parent_1_to_child_1 > > Table_parent_2_to_child_1 > > And the following code do it! > > When I insert data into the tables all running properly, but if I want to > make a query with a filter, the query return me all the records, and not only > the record which I'm searching for. > > It's possible to make a direct query to all tables with a filter? Or is more > usefull realize a class that embed the tables to make a direct query? > > Every suggestion will be helpful. > Thanks in advance. > Best regards. > > Luca > > > [0] > Link > > [1] > from sqlalchemy import create_engine > from sqlalchemy import Table, Column, Integer, String, ForeignKey, Sequence > from sqlalchemy.ext.declarative import declarative_base > from sqlalchemy.orm import sessionmaker, relationship > > > Base = declarative_base() > > > teachers_lessons = Table( > "teachers_lessons", > Base.metadata, > Column("fk_teacher", Integer, ForeignKey("teachers.id")), > Column("fk_lesson", Integer, ForeignKey("lessons.id")), > ) > > aula_lessons = Table( > "aula_lessons", > Base.metadata, > Column("fk_teacher", Integer, ForeignKey("teachers.id")), > Column("fk_aula", Integer, ForeignKey("aula.id")), > ) > > computer_aula = Table( > "computer_aula", > Base.metadata, > Column("fk_computer", Integer, ForeignKey("computer.id")), > Column("fk_aula", Integer, ForeignKey("aula.id")), > ) > > class Teacher(Base): > __tablename__ = "teachers" > > id = Column("id", Integer, Sequence("teachers_id_seq"), > primary_key=True) > name = Column("name", String(50), nullable=False) > > lessons = relationship( > "Lesson", > backref="teachers", > secondary=teachers_lessons > ) > > aula = relationship( > "Aula", > backref="teachers", > secondary=aula_lessons > ) > > class Computer(Base): > __tablename__ = "computer" > > id = Column("id", Integer, Sequence("computer_id_seq"), > primary_key=True) > pc_code = Column("pc_code", String(50), nullable=False) > > aula = relationship( > "Aula", > backref="computer", > secondary=computer_aula > ) > > > class Lesson(Base): > __tablename__ = "lessons" > > id = Column("id", Integer, Sequence("lessons_id_seq"), primary_key=True) > name = Column("name", String(50), nullable=False) > > class Aula(Base): > __tablename__ = "aula" > > id = Column("id", Integer, Sequence("aula_id_seq"), primary_key=True) > nr_aula = Column("nr_aula", String(50), nullable=False) > > > class Schedule(Teacher, Lesson, Aula): > pass > > > engine = create_engine("sqlite:///sqlalchemy_test.db") > Base.metadata.create_all(engine) > Session = sessionmaker(bind=engine) > > > if __name__ == "__main__": > s = Session() > > ## t1 = Teacher(name="gennaro") > ## t1.lessons = [ > ## Lesson(name="gennaro lesson"), > ## ] > ## > ## t1.aula = [ > ## Aula(nr_aula="B2"), > ## ] > ## s.add(t1) > ## > ## s.commit() > ## > ## t1 = Teacher(name="giulio") > ## t1.lessons = [ > ## Lesson(name="giulio lesson 1"), > ## Lesson(name="giulio lesson 2"), > ## ] > ## > ## t1.aula = [ > ## Aula(nr_aula="A3"), > ## ] > ## s.add(t1) > ## > ## s.commit() > ## > ## > ## t1 = Computer(pc_code="pcn1234") > ## t1.aula = [ > ## Aula(nr_aula="B2"), > ## ] > ## s.add(t1) > ## > ## s.commit() > > Session = sessionmaker(bind=engine, autoflush=True, autocommit=True) > session = Session() > data = session.query(Schedule).filter(Aula.nr_aula=='B2') # > > print data > > for i in data: > print i.id, i.name > for i in i.aula: > print i.nr_aula > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at http://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/groups/opt_out.
signature.asc
Description: Message signed with OpenPGP using GPGMail