Python sqlalchemy.true() Examples

The following are 30 code examples of sqlalchemy.true(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy , or try the search function .
Example #1
Source File: test_joins.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_from_function_select_entity_from(self):
        Bookcase = self.classes.Bookcase

        s = Session()

        subq = s.query(Bookcase).subquery()

        srf = lateral(func.generate_series(1, Bookcase.bookcase_shelves))

        self.assert_compile(
            s.query(Bookcase).select_entity_from(subq).join(srf, true()),
            "SELECT anon_1.bookcase_id AS anon_1_bookcase_id, "
            "anon_1.bookcase_owner_id AS anon_1_bookcase_owner_id, "
            "anon_1.bookcase_shelves AS anon_1_bookcase_shelves, "
            "anon_1.bookcase_width AS anon_1_bookcase_width "
            "FROM (SELECT bookcases.bookcase_id AS bookcase_id, "
            "bookcases.bookcase_owner_id AS bookcase_owner_id, "
            "bookcases.bookcase_shelves AS bookcase_shelves, "
            "bookcases.bookcase_width AS bookcase_width FROM bookcases) "
            "AS anon_1 "
            "JOIN LATERAL "
            "generate_series(:generate_series_1, anon_1.bookcase_shelves) "
            "AS anon_2 ON true",
        ) 
Example #2
Source File: test_resultset.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_keyed_accessor_composite_labeled(self, connection):
        keyed1 = self.tables.keyed1
        keyed2 = self.tables.keyed2

        row = connection.execute(
            select([keyed1, keyed2])
            .select_from(keyed1.join(keyed2, true()))
            .apply_labels()
        ).first()
        eq_(row.keyed1_b, "a1")
        eq_(row.keyed1_a, "a1")
        eq_(row.keyed1_q, "c1")
        eq_(row.keyed1_c, "c1")
        eq_(row.keyed2_a, "a2")
        eq_(row.keyed2_b, "b2")

        assert_raises(KeyError, lambda: row["keyed2_c"])
        assert_raises(KeyError, lambda: row["keyed2_q"])
        assert_raises(KeyError, lambda: row._mapping["keyed2_c"])
        assert_raises(KeyError, lambda: row._mapping["keyed2_q"]) 
Example #3
Source File: test_resultset.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_keyed_accessor_composite_keys_precedent(self, connection):
        keyed1 = self.tables.keyed1
        keyed3 = self.tables.keyed3

        row = connection.execute(
            select([keyed1, keyed3]).select_from(keyed1.join(keyed3, true()))
        ).first()
        eq_(row.q, "c1")

        # prior to 1.4 #4887, this raised an "ambiguous column name 'a'""
        # message, because "b" is linked to "a" which is a dupe.  but we know
        # where "b" is in the row by position.
        eq_(row.b, "a1")

        # "a" is of course ambiguous
        assert_raises_message(
            exc.InvalidRequestError,
            "Ambiguous column name 'a'",
            getattr,
            row,
            "a",
        )
        eq_(row.d, "d3") 
Example #4
Source File: test_compiler.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_true_short_circuit(self):
        t = table("t", column("x"))

        self.assert_compile(
            select([t]).where(true()),
            "SELECT t.x FROM t WHERE 1 = 1",
            dialect=default.DefaultDialect(supports_native_boolean=False),
        )
        self.assert_compile(
            select([t]).where(true()),
            "SELECT t.x FROM t WHERE true",
            dialect=default.DefaultDialect(supports_native_boolean=True),
        )

        self.assert_compile(
            select([t]),
            "SELECT t.x FROM t",
            dialect=default.DefaultDialect(supports_native_boolean=True),
        ) 
Example #5
Source File: test_lateral.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_join_lateral_w_select_implicit_subquery(self):
        table1 = self.tables.people
        table2 = self.tables.books

        subq = (
            select([table2.c.book_id])
            .correlate(table1)
            .where(table1.c.people_id == table2.c.book_owner_id)
            .lateral()
        )
        stmt = select([table1, subq.c.book_id]).select_from(
            table1.join(subq, true())
        )

        self.assert_compile(
            stmt,
            "SELECT people.people_id, people.age, people.name, "
            "anon_1.book_id "
            "FROM people JOIN LATERAL (SELECT books.book_id AS book_id "
            "FROM books "
            "WHERE people.people_id = books.book_owner_id) "
            "AS anon_1 ON true",
        ) 
Example #6
Source File: test_lateral.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_join_lateral_w_select_subquery(self):
        table1 = self.tables.people
        table2 = self.tables.books

        subq = (
            select([table2.c.book_id])
            .correlate(table1)
            .where(table1.c.people_id == table2.c.book_owner_id)
            .subquery()
            .lateral()
        )
        stmt = select([table1, subq.c.book_id]).select_from(
            table1.join(subq, true())
        )

        self.assert_compile(
            stmt,
            "SELECT people.people_id, people.age, people.name, anon_1.book_id "
            "FROM people JOIN LATERAL (SELECT books.book_id AS book_id "
            "FROM books "
            "WHERE people.people_id = books.book_owner_id) AS anon_1 ON true",
        ) 
Example #7
Source File: test_froms.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_overlap_plain(self):
        s = Session()
        row = (
            s.query(self.classes.Foo, self.classes.Bar)
            .join(self.classes.Bar, true())
            .all()[0]
        )

        def go():
            eq_(row.Foo.id, 1)
            eq_(row.Foo.bar_id, 2)
            eq_(row.Bar.id, 3)

        # all three columns are loaded independently without
        # overlap, no additional SQL to load all attributes
        self.assert_sql_count(testing.db, go, 0) 
Example #8
Source File: test_froms.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_overlap_subquery(self):
        s = Session()
        row = (
            s.query(self.classes.Foo, self.classes.Bar)
            .join(self.classes.Bar, true())
            .from_self()
            .all()[0]
        )

        def go():
            eq_(row.Foo.id, 1)
            eq_(row.Foo.bar_id, 2)
            eq_(row.Bar.id, 3)

        # all three columns are loaded independently without
        # overlap, no additional SQL to load all attributes
        self.assert_sql_count(testing.db, go, 0) 
Example #9
Source File: test_query.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_nested_future(self):
        User, Address = self.classes.User, self.classes.Address

        s = create_session()

        stmt = future_select(User, Address).join(Address, true()).limit(2)
        eq_(
            s.scalar(future_select(func.count()).select_from(stmt.subquery())),
            2,
        )

        stmt = future_select(User, Address).join(Address, true()).limit(100)
        eq_(
            s.scalar(future_select(func.count()).select_from(stmt.subquery())),
            20,
        )

        stmt = future_select(User, Address).join(Address).limit(100)
        eq_(
            s.scalar(future_select(func.count()).select_from(stmt.subquery())),
            5,
        ) 
Example #10
Source File: test_query.py    From sqlalchemy with MIT License 6 votes vote down vote up
def test_cols(self):
        """test that column-based queries always nest."""

        User, Address = self.classes.User, self.classes.Address

        s = create_session()

        q = s.query(func.count(distinct(User.name)))
        eq_(q.count(), 1)

        q = s.query(func.count(distinct(User.name))).distinct()
        eq_(q.count(), 1)

        q = s.query(User.name)
        eq_(q.count(), 4)

        q = s.query(User.name, Address).join(Address, true())
        eq_(q.count(), 20)

        q = s.query(Address.user_id)
        eq_(q.count(), 5)
        eq_(q.distinct().count(), 3) 
Example #11
Source File: test_query.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_multiple_entity(self):
        User, Address = self.classes.User, self.classes.Address

        s = create_session()
        q = s.query(User, Address).join(Address, true())
        eq_(q.count(), 20)  # cartesian product

        q = s.query(User, Address).join(User.addresses)
        eq_(q.count(), 5) 
Example #12
Source File: test_query.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_nested(self):
        User, Address = self.classes.User, self.classes.Address

        s = create_session()
        q = s.query(User, Address).join(Address, true()).limit(2)
        eq_(q.count(), 2)

        q = s.query(User, Address).join(Address, true()).limit(100)
        eq_(q.count(), 20)

        q = s.query(User, Address).join(User.addresses).limit(100)
        eq_(q.count(), 5) 
Example #13
Source File: test_bind.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_bind_selectable_join(self, two_table_fixture):
        session, base_class_bind, concrete_sub_bind = two_table_fixture

        stmt = self.tables.base_table.join(
            self.tables.concrete_sub_table, true()
        )
        is_(session.get_bind(clause=stmt), base_class_bind) 
Example #14
Source File: 282e6b269222_create_device_name_lat_long_enabled_and_otaa_.py    From floranet with MIT License 5 votes vote down vote up
def upgrade():
    op.add_column('devices',
        sa.Column('name', sa.String(), nullable=False, server_default='device'))
    op.add_column('devices',
        sa.Column('enabled', sa.Boolean(), nullable=False, server_default=sa.true()))
    op.add_column('devices',
        sa.Column('otaa', sa.Boolean(), nullable=False, server_default=sa.true()))
    op.add_column('devices',
        sa.Column('latitude', sa.Float(), nullable=True))
    op.add_column('devices',
        sa.Column('longitude', sa.Float(), nullable=True)) 
Example #15
Source File: test_joins.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_select_subquery_sef_explicit_correlate(self):
        Person, Book = self.classes("Person", "Book")

        s = Session()

        stmt = s.query(Person).subquery()

        subq = (
            s.query(Book.book_id)
            .correlate(Person)
            .filter(Person.people_id == Book.book_owner_id)
            .subquery()
            .lateral()
        )

        stmt = (
            s.query(Person, subq.c.book_id)
            .select_entity_from(stmt)
            .join(subq, true())
        )

        self.assert_compile(
            stmt,
            "SELECT anon_1.people_id AS anon_1_people_id, "
            "anon_1.age AS anon_1_age, anon_1.name AS anon_1_name, "
            "anon_2.book_id AS anon_2_book_id "
            "FROM "
            "(SELECT people.people_id AS people_id, people.age AS age, "
            "people.name AS name FROM people) AS anon_1 "
            "JOIN LATERAL "
            "(SELECT books.book_id AS book_id FROM books "
            "WHERE anon_1.people_id = books.book_owner_id) AS anon_2 ON true",
        ) 
Example #16
Source File: test_query.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_columns_augmented_roundtrip_three_from_self(self):
        """Test workaround for legacy style DISTINCT on extra column.

        See #5134

        """

        User, Address = self.classes.User, self.classes.Address

        sess = create_session()

        q = (
            sess.query(
                User.id,
                User.name.label("foo"),
                Address.id,
                Address.email_address,
            )
            .join(Address, true())
            .filter(User.name == "jack")
            .filter(User.id + Address.user_id > 0)
            .distinct()
            .from_self(User.id, User.name.label("foo"), Address.id)
            .order_by(User.id, User.name, Address.email_address)
        )

        eq_(
            q.all(),
            [
                (7, "jack", 3),
                (7, "jack", 4),
                (7, "jack", 2),
                (7, "jack", 5),
                (7, "jack", 1),
            ],
        )
        for row in q:
            eq_(row._mapping.keys(), ["id", "foo", "id"]) 
Example #17
Source File: test_joins.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_select_subquery_sef_explicit_correlate_coreonly(self):
        Person, Book = self.classes("Person", "Book")

        s = Session()

        stmt = s.query(Person).subquery()

        subq = (
            select([Book.book_id])
            .correlate(Person)
            .where(Person.people_id == Book.book_owner_id)
            .subquery()
            .lateral()
        )

        stmt = (
            s.query(Person, subq.c.book_id)
            .select_entity_from(stmt)
            .join(subq, true())
        )

        self.assert_compile(
            stmt,
            "SELECT anon_1.people_id AS anon_1_people_id, "
            "anon_1.age AS anon_1_age, anon_1.name AS anon_1_name, "
            "anon_2.book_id AS anon_2_book_id "
            "FROM "
            "(SELECT people.people_id AS people_id, people.age AS age, "
            "people.name AS name FROM people) AS anon_1 "
            "JOIN LATERAL "
            "(SELECT books.book_id AS book_id FROM books "
            "WHERE anon_1.people_id = books.book_owner_id) AS anon_2 ON true",
        ) 
Example #18
Source File: test_joins.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_select_subquery_sef_implicit_correlate_coreonly(self):
        Person, Book = self.classes("Person", "Book")

        s = Session()

        stmt = s.query(Person).subquery()

        subq = (
            select([Book.book_id])
            .where(Person.people_id == Book.book_owner_id)
            .subquery()
            .lateral()
        )

        stmt = (
            s.query(Person, subq.c.book_id)
            .select_entity_from(stmt)
            .join(subq, true())
        )

        self.assert_compile(
            stmt,
            "SELECT anon_1.people_id AS anon_1_people_id, "
            "anon_1.age AS anon_1_age, anon_1.name AS anon_1_name, "
            "anon_2.book_id AS anon_2_book_id "
            "FROM "
            "(SELECT people.people_id AS people_id, people.age AS age, "
            "people.name AS name FROM people) AS anon_1 "
            "JOIN LATERAL "
            "(SELECT books.book_id AS book_id FROM books "
            "WHERE anon_1.people_id = books.book_owner_id) AS anon_2 ON true",
        ) 
Example #19
Source File: f0b00081fda9_uploads_persistent_global.py    From docassemble with MIT License 5 votes vote down vote up
def upgrade():
    op.add_column(dbtableprefix + 'uploads', sa.Column('private', sa.Boolean, server_default=sa.true()))
    op.add_column(dbtableprefix + 'uploads', sa.Column('persistent', sa.Boolean, server_default=sa.false())) 
Example #20
Source File: test_joins.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_select_subquery_sef_implicit_correlate(self):
        Person, Book = self.classes("Person", "Book")

        s = Session()

        stmt = s.query(Person).subquery()

        subq = (
            s.query(Book.book_id)
            .filter(Person.people_id == Book.book_owner_id)
            .subquery()
            .lateral()
        )

        stmt = (
            s.query(Person, subq.c.book_id)
            .select_entity_from(stmt)
            .join(subq, true())
        )

        self.assert_compile(
            stmt,
            "SELECT anon_1.people_id AS anon_1_people_id, "
            "anon_1.age AS anon_1_age, anon_1.name AS anon_1_name, "
            "anon_2.book_id AS anon_2_book_id "
            "FROM "
            "(SELECT people.people_id AS people_id, people.age AS age, "
            "people.name AS name FROM people) AS anon_1 "
            "JOIN LATERAL "
            "(SELECT books.book_id AS book_id FROM books "
            "WHERE anon_1.people_id = books.book_owner_id) AS anon_2 ON true",
        ) 
Example #21
Source File: test_joins.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_select_subquery(self):
        Person, Book = self.classes("Person", "Book")

        s = Session()

        subq = (
            s.query(Book.book_id)
            .correlate(Person)
            .filter(Person.people_id == Book.book_owner_id)
            .subquery()
            .lateral()
        )

        stmt = s.query(Person, subq.c.book_id).join(subq, true())

        self.assert_compile(
            stmt,
            "SELECT people.people_id AS people_people_id, "
            "people.age AS people_age, people.name AS people_name, "
            "anon_1.book_id AS anon_1_book_id "
            "FROM people JOIN LATERAL "
            "(SELECT books.book_id AS book_id FROM books "
            "WHERE people.people_id = books.book_owner_id) AS anon_1 ON true",
        )

    # sef == select_entity_from 
Example #22
Source File: test_polymorphic_rel.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_mixed_entities_four(self):
        sess = create_session()
        palias = aliased(Person)
        expected = [
            (
                Engineer(
                    status="regular engineer",
                    engineer_name="dilbert",
                    name="dilbert",
                    company_id=1,
                    primary_language="java",
                    person_id=1,
                    type="engineer",
                ),
                "Elbonia, Inc.",
                Engineer(
                    status="elbonian engineer",
                    engineer_name="vlad",
                    name="vlad",
                    primary_language="cobol",
                ),
            )
        ]

        eq_(
            sess.query(palias, Company.name, Person)
            .select_from(join(palias, Company, true()))
            .join(Company.employees)
            .filter(Company.name == "Elbonia, Inc.")
            .filter(palias.name == "dilbert")
            .all(),
            expected,
        ) 
Example #23
Source File: 12d5dcd9a231_add_device_adr_flag.py    From floranet with MIT License 5 votes vote down vote up
def upgrade():
    op.add_column('devices',
        sa.Column('adr', sa.Boolean(), nullable=False, server_default=sa.true())) 
Example #24
Source File: 0d78d545906f_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    op.add_column('departments', sa.Column('is_public_assaults_on_officers', sa.Boolean(), server_default=sa.true(), nullable=False))
    op.add_column('departments', sa.Column('is_public_citizen_complaints', sa.Boolean(), server_default=sa.true(), nullable=False))
    op.add_column('departments', sa.Column('is_public_officer_involved_shootings', sa.Boolean(), server_default=sa.true(), nullable=False))
    op.add_column('departments', sa.Column('is_public_use_of_force_incidents', sa.Boolean(), server_default=sa.true(), nullable=False)) 
Example #25
Source File: 4610803bdf0d_router_assoc_add_advertise_extra_routes.py    From networking-bgpvpn with Apache License 2.0 5 votes vote down vote up
def upgrade():
    op.add_column('bgpvpn_router_associations',
                  sa.Column('advertise_extra_routes',
                            sa.Boolean(), nullable=False,
                            server_default=sa.true())) 
Example #26
Source File: 6d30846080b2_.py    From comport with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.add_column('departments', sa.Column('is_public', sa.Boolean(), server_default=sa.true(), nullable=False))
    ### end Alembic commands ### 
Example #27
Source File: 802322a84154_add_stream_delay_prefs.py    From lrrbot with Apache License 2.0 5 votes vote down vote up
def upgrade():
	alembic.op.add_column('users',
		sqlalchemy.Column('stream_delay', sqlalchemy.Integer, nullable=False, server_default='10')
	)
	alembic.op.add_column('users',
		sqlalchemy.Column('chat_timestamps', sqlalchemy.Integer, nullable=False, server_default='0')
	)
	alembic.op.add_column('users',
		sqlalchemy.Column('chat_timestamps_24hr', sqlalchemy.Boolean, nullable=False, server_default=sqlalchemy.true())
	)
	alembic.op.add_column('users',
		sqlalchemy.Column('chat_timestamps_secs', sqlalchemy.Boolean, nullable=False, server_default=sqlalchemy.false())
	) 
Example #28
Source File: 33ae817a1ff4_add_kubernetes_resource_checkpointing.py    From airflow with Apache License 2.0 5 votes vote down vote up
def upgrade():   # noqa: D103
    columns_and_constraints = [
        sa.Column("one_row_id", sa.Boolean, server_default=sa.true(), primary_key=True),
        sa.Column("resource_version", sa.String(255))
    ]

    conn = op.get_bind()

    # alembic creates an invalid SQL for mssql and mysql dialects
    if conn.dialect.name in {"mysql"}:
        columns_and_constraints.append(
            sa.CheckConstraint("one_row_id<>0", name="kube_resource_version_one_row_id")
        )
    elif conn.dialect.name not in {"mssql"}:
        columns_and_constraints.append(
            sa.CheckConstraint("one_row_id", name="kube_resource_version_one_row_id")
        )

    table = op.create_table(
        RESOURCE_TABLE,
        *columns_and_constraints
    )

    op.bulk_insert(table, [
        {"resource_version": ""}
    ]) 
Example #29
Source File: 86770d1215c0_add_kubernetes_scheduler_uniqueness.py    From airflow with Apache License 2.0 5 votes vote down vote up
def upgrade():   # noqa: D103

    columns_and_constraints = [
        sa.Column("one_row_id", sa.Boolean, server_default=sa.true(), primary_key=True),
        sa.Column("worker_uuid", sa.String(255))
    ]

    conn = op.get_bind()

    # alembic creates an invalid SQL for mssql and mysql dialects
    if conn.dialect.name in {"mysql"}:
        columns_and_constraints.append(
            sa.CheckConstraint("one_row_id<>0", name="kube_worker_one_row_id")
        )
    elif conn.dialect.name not in {"mssql"}:
        columns_and_constraints.append(
            sa.CheckConstraint("one_row_id", name="kube_worker_one_row_id")
        )

    table = op.create_table(
        RESOURCE_TABLE,
        *columns_and_constraints
    )

    op.bulk_insert(table, [
        {"worker_uuid": ""}
    ]) 
Example #30
Source File: db_query.py    From networking-l2gw with Apache License 2.0 5 votes vote down vote up
def _model_query(self, context, model):
        """Query model based on filter."""
        query = context.session.query(model)
        query_filter = None
        if not context.is_admin and hasattr(model, 'tenant_id'):
            if hasattr(model, 'shared'):
                query_filter = ((model.tenant_id == context.tenant_id) |
                                (model.shared == sa.true()))
            else:
                query_filter = (model.tenant_id == context.tenant_id)
        if query_filter is not None:
            query = query.filter(query_filter)
        return query