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<https://www.google.it/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CDMQFjAA&url=http%3A%2F%2Fxsnippet.org%2F359350%2F&ei=eKSTUrWBLtSg7AaB54HACQ&usg=AFQjCNF1qYFBs_KgwldY5u5ipGvLdTxO2A&sig2=HbP2b94GH2fyOuEDlZWP8A&bvm=bv.57127890,d.ZGU> [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 [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
