Here are the examples of the python api sqlalchemy.Computed taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
35 Examples
3
Source : test_compiler.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_column_computed(self, text, persisted):
m = MetaData()
kwargs = {"persisted": persisted} if persisted != "ignore" else {}
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", **kwargs)),
)
self.assert_compile(
schema.CreateTable(t),
"CREATE TABLE t (x INTEGER NULL, y AS (x + 2)%s)" % text,
)
class SchemaTest(fixtures.TestBase):
3
Source : test_compiler.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_column_computed(self, text, persisted):
m = MetaData()
kwargs = {"persisted": persisted} if persisted != "ignore" else {}
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", **kwargs)),
)
self.assert_compile(
schema.CreateTable(t),
"CREATE TABLE t (x INTEGER, y INTEGER GENERATED "
"ALWAYS AS (x + 2)%s)" % text,
)
class SQLTest(fixtures.TestBase, AssertsCompiledSQL):
3
Source : test_compiler.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_returning_insert_computed(self):
m = MetaData()
t1 = Table(
"t1",
m,
Column("id", Integer, primary_key=True),
Column("foo", Integer),
Column("bar", Integer, Computed("foo + 42")),
)
self.assert_compile(
t1.insert().values(id=1, foo=5).returning(t1.c.bar),
"INSERT INTO t1 (id, foo) VALUES (:id, :foo) "
"RETURNING t1.bar INTO :ret_0",
)
def test_returning_update_computed_warning(self):
3
Source : test_compiler.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_returning_update_computed_warning(self):
m = MetaData()
t1 = Table(
"t1",
m,
Column("id", Integer, primary_key=True),
Column("foo", Integer),
Column("bar", Integer, Computed("foo + 42")),
)
with testing.expect_warnings(
"Computed columns don't work with Oracle UPDATE"
):
self.assert_compile(
t1.update().values(id=1, foo=5).returning(t1.c.bar),
"UPDATE t1 SET id=:id, foo=:foo RETURNING t1.bar INTO :ret_0",
)
def test_compound(self):
3
Source : test_compiler.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_column_computed(self, text, persisted):
m = MetaData()
kwargs = {"persisted": persisted} if persisted != "ignore" else {}
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", **kwargs)),
)
self.assert_compile(
schema.CreateTable(t),
"CREATE TABLE t (x INTEGER, y INTEGER GENERATED "
"ALWAYS AS (x + 2)%s)" % text,
)
def test_column_computed_persisted_true(self):
3
Source : test_compiler.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_column_computed_persisted_true(self):
m = MetaData()
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", persisted=True)),
)
assert_raises_message(
exc.CompileError,
r".*Oracle computed columns do not support 'stored' ",
schema.CreateTable(t).compile,
dialect=oracle.dialect(),
)
class SequenceTest(fixtures.TestBase, AssertsCompiledSQL):
3
Source : test_dialect.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def define_tables(cls, metadata):
Table(
"test",
metadata,
Column("id", Integer, primary_key=True),
Column("foo", Integer),
Column("bar", Integer, Computed("foo + 42")),
)
Table(
"test_no_returning",
metadata,
Column("id", Integer, primary_key=True),
Column("foo", Integer),
Column("bar", Integer, Computed("foo + 42")),
implicit_returning=False,
)
def test_computed_insert(self):
3
Source : test_compiler.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_column_computed(self, text, persisted):
m = MetaData()
kwargs = {"persisted": persisted} if persisted != "ignore" else {}
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", **kwargs)),
)
self.assert_compile(
schema.CreateTable(t),
"CREATE TABLE t (x INTEGER, y INTEGER GENERATED "
"ALWAYS AS (x + 2)%s)" % text,
)
def test_column_computed_persisted_false(self):
3
Source : test_compiler.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_column_computed_persisted_false(self):
m = MetaData()
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", persisted=False)),
)
assert_raises_message(
exc.CompileError,
"PostrgreSQL computed columns do not support 'virtual'",
schema.CreateTable(t).compile,
dialect=postgresql.dialect(),
)
class InsertOnConflictTest(fixtures.TestBase, AssertsCompiledSQL):
3
Source : test_firebird.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_column_computed(self, persisted):
m = MetaData()
kwargs = {"persisted": persisted} if persisted != "ignore" else {}
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", **kwargs)),
)
self.assert_compile(
schema.CreateTable(t),
"CREATE TABLE t (x INTEGER, y INTEGER GENERATED "
"ALWAYS AS (x + 2))",
)
@testing.combinations(
3
Source : test_firebird.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_column_computed_raises(self, persisted):
m = MetaData()
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", persisted=persisted)),
)
assert_raises_message(
exc.CompileError,
"Firebird computed columns do not support a persistence method",
schema.CreateTable(t).compile,
dialect=firebird.dialect(),
)
class TypesTest(fixtures.TestBase):
3
Source : test_sqlite.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_column_computed(self, persisted):
m = MetaData()
kwargs = {"persisted": persisted} if persisted != "ignore" else {}
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", **kwargs)),
)
assert_raises_message(
exc.CompileError,
"SQLite does not support computed columns",
schema.CreateTable(t).compile,
dialect=sqlite.dialect(),
)
class AttachedDBTest(fixtures.TestBase):
3
Source : test_defaults.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def define_tables(cls, metadata):
Table(
"test",
metadata,
Column("id", Integer, primary_key=True),
Column("foo", Integer),
Column("bar", Integer, Computed("foo + 42")),
)
@classmethod
3
Source : test_computed.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_column_computed(self, text, persisted):
m = MetaData()
kwargs = {"persisted": persisted} if persisted != "ignore" else {}
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", **kwargs)),
)
self.assert_compile(
CreateTable(t),
"CREATE TABLE t (x INTEGER, y INTEGER GENERATED "
"ALWAYS AS (x + 2)%s)" % text,
)
def test_server_default_onupdate(self):
3
Source : test_computed.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_server_default_onupdate(self):
text = (
"A generated column cannot specify a server_default or a "
"server_onupdate argument"
)
def fn(**kwargs):
m = MetaData()
Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2"), **kwargs),
)
assert_raises_message(ArgumentError, text, fn, server_default="42")
assert_raises_message(ArgumentError, text, fn, server_onupdate="42")
def test_tometadata(self):
3
Source : test_autogen_render.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_render_add_column_computed(self):
c = sa.Computed("5")
op_obj = ops.AddColumnOp("foo", Column("x", Integer, c))
eq_ignore_whitespace(
autogenerate.render_op_text(self.autogen_context, op_obj),
"op.add_column('foo', sa.Column('x', sa.Integer(), "
"sa.Computed('5', ), nullable=True))",
)
@config.requirements.computed_columns_api
3
Source : test_autogen_render.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_render_add_column_computed_persisted(self, persisted):
op_obj = ops.AddColumnOp(
"foo", Column("x", Integer, sa.Computed("5", persisted=persisted))
)
eq_ignore_whitespace(
autogenerate.render_op_text(self.autogen_context, op_obj),
"op.add_column('foo', sa.Column('x', sa.Integer(), "
"sa.Computed('5', persisted=%s), nullable=True))" % persisted,
)
@config.requirements.computed_columns_api
3
Source : test_autogen_render.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_render_alter_column_computed_modify_default(self):
op_obj = ops.AlterColumnOp(
"sometable", "somecolumn", modify_server_default=sa.Computed("7")
)
eq_ignore_whitespace(
autogenerate.render_op_text(self.autogen_context, op_obj),
"op.alter_column('sometable', 'somecolumn', "
"server_default=sa.Computed('7', ))",
)
@config.requirements.computed_columns_api
3
Source : test_autogen_render.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_render_alter_column_computed_existing_default(self):
op_obj = ops.AlterColumnOp(
"sometable",
"somecolumn",
existing_server_default=sa.Computed("42"),
)
eq_ignore_whitespace(
autogenerate.render_op_text(self.autogen_context, op_obj),
"op.alter_column('sometable', 'somecolumn', "
"existing_server_default=sa.Computed('42', ))",
)
@config.requirements.computed_columns_api
3
Source : test_autogen_render.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_render_alter_column_computed_modify_default_perisisted(
self, persisted
):
op_obj = ops.AlterColumnOp(
"sometable",
"somecolumn",
modify_server_default=sa.Computed("7", persisted=persisted),
)
eq_ignore_whitespace(
autogenerate.render_op_text(self.autogen_context, op_obj),
"op.alter_column('sometable', 'somecolumn', server_default"
"=sa.Computed('7', persisted=%s))" % persisted,
)
@config.requirements.computed_columns_api
3
Source : test_autogen_render.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_render_alter_column_computed_existing_default_perisisted(
self, persisted
):
c = sa.Computed("42", persisted=persisted)
op_obj = ops.AlterColumnOp(
"sometable", "somecolumn", existing_server_default=c
)
eq_ignore_whitespace(
autogenerate.render_op_text(self.autogen_context, op_obj),
"op.alter_column('sometable', 'somecolumn', "
"existing_server_default=sa.Computed('42', persisted=%s))"
% persisted,
)
@config.requirements.identity_columns_api
3
Source : test_compiler.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_column_computed(self, text, persisted):
m = MetaData()
kwargs = {"persisted": persisted} if persisted != "ignore" else {}
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", **kwargs)),
)
self.assert_compile(
schema.CreateTable(t),
"CREATE TABLE t (x INTEGER NULL, y AS (x + 2)%s)" % text,
)
@testing.combinations(
3
Source : test_compiler.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_column_computed_persisted_true(self):
m = MetaData()
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", persisted=True)),
)
assert_raises_message(
exc.CompileError,
r".*Oracle computed columns do not support 'stored' ",
schema.CreateTable(t).compile,
dialect=oracle.dialect(),
)
def test_column_identity(self):
3
Source : test_dialect.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def define_tables(cls, metadata):
Table(
"test",
metadata,
Column("id", Integer, primary_key=True),
Column("foo", Integer),
Column("bar", Integer, Computed("foo + 42")),
)
Table(
"test_no_returning",
metadata,
Column("id", Integer, primary_key=True),
Column("foo", Integer),
Column("bar", Integer, Computed("foo + 42")),
implicit_returning=False,
)
def test_computed_insert(self, connection):
3
Source : test_compiler.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_column_computed_persisted_false(self):
m = MetaData()
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", persisted=False)),
)
assert_raises_message(
exc.CompileError,
"PostrgreSQL computed columns do not support 'virtual'",
schema.CreateTable(t).compile,
dialect=postgresql.dialect(),
)
@testing.combinations(True, False)
3
Source : test_sqlite.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_column_computed(self, text, persisted):
m = MetaData()
kwargs = {"persisted": persisted} if persisted != "ignore" else {}
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", **kwargs)),
)
self.assert_compile(
schema.CreateTable(t),
"CREATE TABLE t (x INTEGER,"
" y INTEGER GENERATED ALWAYS AS (x + 2)%s)" % text,
)
class AttachedDBTest(fixtures.TestBase):
3
Source : test_computed.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_column_computed(self, text, persisted):
m = MetaData()
kwargs = {"persisted": persisted} if persisted != "ignore" else {}
t = Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2", **kwargs)),
)
self.assert_compile(
CreateTable(t),
"CREATE TABLE t (x INTEGER, y INTEGER GENERATED "
"ALWAYS AS (x + 2)%s)" % text,
)
def test_other_options(self):
3
Source : test_computed.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_other_options(self):
t = Table(
"t",
MetaData(),
Column(
"y", Integer, Computed("x + 2"), nullable=False, unique=True
),
)
self.assert_compile(
CreateTable(t),
"CREATE TABLE t ("
"y INTEGER GENERATED ALWAYS AS (x + 2) NOT NULL, UNIQUE (y))",
)
def test_server_default_onupdate(self):
3
Source : test_computed.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_server_default_onupdate(self):
text = (
"A generated column cannot specify a server_default or a "
"server_onupdate argument"
)
def fn(**kwargs):
m = MetaData()
Table(
"t",
m,
Column("x", Integer),
Column("y", Integer, Computed("x + 2"), **kwargs),
)
assert_raises_message(ArgumentError, text, fn, server_default="42")
assert_raises_message(ArgumentError, text, fn, server_onupdate="42")
def test_to_metadata(self):
0
Source : test_computed.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_tometadata(self):
comp1 = Computed("x + 2")
m = MetaData()
t = Table("t", m, Column("x", Integer), Column("y", Integer, comp1))
is_(comp1.column, t.c.y)
is_(t.c.y.server_onupdate, comp1)
is_(t.c.y.server_default, comp1)
m2 = MetaData()
t2 = t.tometadata(m2)
comp2 = t2.c.y.server_default
is_not_(comp1, comp2)
is_(comp1.column, t.c.y)
is_(t.c.y.server_onupdate, comp1)
is_(t.c.y.server_default, comp1)
is_(comp2.column, t2.c.y)
is_(t2.c.y.server_onupdate, comp2)
is_(t2.c.y.server_default, comp2)
0
Source : test_autogen_computed.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_add_computed_column(self):
m1 = MetaData()
m2 = MetaData()
Table("user", m1, Column("id", Integer, primary_key=True))
Table(
"user",
m2,
Column("id", Integer, primary_key=True),
Column("foo", Integer, sa.Computed("5")),
)
diffs = self._fixture(m1, m2)
eq_(diffs[0][0], "add_column")
eq_(diffs[0][2], "user")
eq_(diffs[0][3].name, "foo")
c = diffs[0][3].computed
is_true(isinstance(c, sa.Computed))
is_(c.persisted, None)
eq_(str(c.sqltext), "5")
def test_remove_computed_column(self):
0
Source : test_autogen_computed.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_remove_computed_column(self):
m1 = MetaData()
m2 = MetaData()
Table(
"user",
m1,
Column("id", Integer, primary_key=True),
Column("foo", Integer, sa.Computed("5")),
)
Table("user", m2, Column("id", Integer, primary_key=True))
diffs = self._fixture(m1, m2)
eq_(diffs[0][0], "remove_column")
eq_(diffs[0][2], "user")
c = diffs[0][3]
eq_(c.name, "foo")
if config.requirements.computed_reflects_normally.enabled:
is_true(isinstance(c.computed, sa.Computed))
else:
is_(c.computed, None)
if config.requirements.computed_reflects_as_server_default.enabled:
is_true(isinstance(c.server_default, sa.DefaultClause))
eq_(str(c.server_default.arg.text), "5")
elif config.requirements.computed_reflects_normally.enabled:
is_true(isinstance(c.computed, sa.Computed))
else:
is_(c.computed, None)
@testing.combinations(
0
Source : test_autogen_computed.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_remove_computed_default_on_computed(self):
"""Asserts the current behavior which is that on PG and Oracle,
the GENERATED ALWAYS AS is reflected as a server default which we can't
tell is actually "computed", so these come out as a modification to
the server default.
"""
m1 = MetaData()
m2 = MetaData()
Table(
"user",
m1,
Column("id", Integer, primary_key=True),
Column("bar", Integer),
Column("foo", Integer, sa.Computed("bar + 42")),
)
Table(
"user",
m2,
Column("id", Integer, primary_key=True),
Column("bar", Integer),
Column("foo", Integer),
)
diffs = self._fixture(m1, m2)
eq_(diffs[0][0][0], "modify_default")
eq_(diffs[0][0][2], "user")
eq_(diffs[0][0][3], "foo")
old = diffs[0][0][-2]
new = diffs[0][0][-1]
is_(new, None)
is_true(isinstance(old, sa.DefaultClause))
if exclusions.against(config, "postgresql"):
eq_(str(old.arg.text), "(bar + 42)")
elif exclusions.against(config, "oracle"):
eq_(str(old.arg.text), '"BAR"+42')
0
Source : test_computed.py
with MIT License
from sqlalchemy
with MIT License
from sqlalchemy
def test_to_metadata(self):
comp1 = Computed("x + 2")
m = MetaData()
t = Table("t", m, Column("x", Integer), Column("y", Integer, comp1))
is_(comp1.column, t.c.y)
is_(t.c.y.server_onupdate, comp1)
is_(t.c.y.server_default, comp1)
m2 = MetaData()
t2 = t.to_metadata(m2)
comp2 = t2.c.y.server_default
is_not(comp1, comp2)
is_(comp1.column, t.c.y)
is_(t.c.y.server_onupdate, comp1)
is_(t.c.y.server_default, comp1)
is_(comp2.column, t2.c.y)
is_(t2.c.y.server_onupdate, comp2)
is_(t2.c.y.server_default, comp2)
0
Source : 3655e2e3bf1a_add_full_text_search.py
with Apache License 2.0
from tech4germany
with Apache License 2.0
from tech4germany
def upgrade():
op.add_column('content_items', sa.Column('search_tsv', postgresql.TSVECTOR(), sa.Computed("""
setweight(to_tsvector('german',
coalesce(content_items.name, '') || ' ' ||
coalesce(content_items.title, '')),
'A') ||
setweight(to_tsvector('german',
coalesce(content_items.body, '')),
'B')
"""), nullable=True))
op.add_column('laws', sa.Column('search_tsv', postgresql.TSVECTOR(), sa.Computed("""
setweight(to_tsvector('german',
coalesce(laws.title_long, '') || ' ' ||
coalesce(laws.title_short, '') || ' ' ||
coalesce(laws.abbreviation, '')),
'A') ||
setweight(to_tsvector('german',
coalesce(laws.notes_body, '')),
'B')
"""), nullable=True))
op.create_index('ix_content_items_search_tsv', 'content_items', ['search_tsv'], unique=False, postgresql_using='gin')
op.create_index('ix_laws_search_tsv', 'laws', ['search_tsv'], unique=False, postgresql_using='gin')
def downgrade():