sqlalchemy.orm.with_polymorphic - python examples

Here are the examples of the python api sqlalchemy.orm.with_polymorphic taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

29 Examples 7

3 View Complete Implementation : test_with_poly.py
Copyright MIT License
Author : sqlalchemy
    def test_join_base_to_sub(self):
        sess = create_session()
        pa = with_polymorphic(Person, [Engineer])

        def go():
            eq_(
                sess.query(pa)
                .filter(pa.Engineer.primary_language == "java")
                .all(),
                self._emps_wo_relationships_fixture()[0:1],
            )

        self.astert_sql_count(testing.db, go, 1)

3 View Complete Implementation : test_with_poly.py
Copyright MIT License
Author : sqlalchemy
    def test_col_expression_base_plus_two_subs(self):
        sess = create_session()
        pa = with_polymorphic(Person, [Engineer, Manager])

        eq_(
            sess.query(
                pa.name, pa.Engineer.primary_language, pa.Manager.manager_name
            )
            .filter(
                or_(
                    pa.Engineer.primary_language == "java",
                    pa.Manager.manager_name == "dogbert",
                )
            )
            .order_by(pa.Engineer.type)
            .all(),
            [("dilbert", "java", None), ("dogbert", None, "dogbert")],
        )

3 View Complete Implementation : test_of_type.py
Copyright MIT License
Author : sqlalchemy
    def test_with_polymorphic_join_compile_one(self):
        sess = Session()

        self.astert_compile(
            sess.query(Company).join(
                Company.employees.of_type(
                    with_polymorphic(
                        Person, [Engineer, Manager], aliased=True, flat=True
                    )
                )
            ),
            "SELECT companies.company_id AS companies_company_id, "
            "companies.name AS companies_name FROM companies "
            "JOIN %s" % (self._polymorphic_join_target([Engineer, Manager])),
        )

3 View Complete Implementation : test_of_type.py
Copyright MIT License
Author : sqlalchemy
    def test_with_polymorphic_join_exec_contains_eager_one(self):
        sess = Session()

        def go():
            wp = with_polymorphic(
                Person, [Engineer, Manager], aliased=True, flat=True
            )
            eq_(
                sess.query(Company)
                .join(Company.employees.of_type(wp))
                .order_by(Company.company_id, wp.person_id)
                .options(contains_eager(Company.employees.of_type(wp)))
                .all(),
                [self.c1, self.c2],
            )

        self.astert_sql_count(testing.db, go, 1)

3 View Complete Implementation : test_of_type.py
Copyright MIT License
Author : sqlalchemy
    def test_with_polymorphic_join_exec_contains_eager_two(self):
        sess = Session()

        def go():
            wp = with_polymorphic(Person, [Engineer, Manager], aliased=True)
            eq_(
                sess.query(Company)
                .join(Company.employees.of_type(wp))
                .order_by(Company.company_id, wp.person_id)
                .options(contains_eager(Company.employees, alias=wp))
                .all(),
                [self.c1, self.c2],
            )

        self.astert_sql_count(testing.db, go, 1)

3 View Complete Implementation : test_of_type.py
Copyright MIT License
Author : sqlalchemy
    def test_with_polymorphic_any(self):
        sess = Session()
        wp = with_polymorphic(Person, [Engineer], aliased=True)
        eq_(
            sess.query(Company.company_id)
            .filter(
                Company.employees.of_type(wp).any(
                    wp.Engineer.primary_language == "java"
                )
            )
            .all(),
            [(1,)],
        )

3 View Complete Implementation : test_of_type.py
Copyright MIT License
Author : sqlalchemy
    def test_subqueryload_explicit_withpoly(self):
        sess = Session()

        def go():
            target = with_polymorphic(Person, Engineer)
            eq_(
                sess.query(Company)
                .filter_by(company_id=1)
                .options(subqueryload(Company.employees.of_type(target)))
                .all(),
                [self._company_with_emps_fixture()[0]],
            )

        self.astert_sql_count(testing.db, go, 4)

3 View Complete Implementation : test_of_type.py
Copyright MIT License
Author : sqlalchemy
    def test_joinedload_explicit_withpoly(self):
        sess = Session()

        def go():
            target = with_polymorphic(Person, Engineer, flat=True)
            eq_(
                sess.query(Company)
                .filter_by(company_id=1)
                .options(joinedload(Company.employees.of_type(target)))
                .all(),
                [self._company_with_emps_fixture()[0]],
            )

        self.astert_sql_count(testing.db, go, 3)

3 View Complete Implementation : test_of_type.py
Copyright MIT License
Author : sqlalchemy
    def test_joinedload_explicit_with_unaliased_poly_compile(self):
        sess = Session()
        target = with_polymorphic(Person, Engineer)
        q = (
            sess.query(Company)
            .filter_by(company_id=1)
            .options(joinedload(Company.employees.of_type(target)))
        )
        astert_raises_message(
            sa_exc.InvalidRequestError,
            "Detected unaliased columns when generating joined load.",
            q._compile_context,
        )

3 View Complete Implementation : test_of_type.py
Copyright MIT License
Author : sqlalchemy
    def test_joinedload_wpoly(self):
        DataContainer, Job, SubJob = (
            self.clastes.DataContainer,
            self.clastes.Job,
            self.clastes.SubJob,
        )

        Job_P = with_polymorphic(Job, SubJob, aliased=True)

        s = Session(testing.db)
        q = s.query(DataContainer).options(
            joinedload(DataContainer.jobs.of_type(Job_P))
        )

        def go():
            eq_(q.all(), self._dc_fixture())

        self.astert_sql_count(testing.db, go, 5)