本文整理匯總了Python中sqlalchemy.schema.Index方法的典型用法代碼示例。如果您正苦於以下問題:Python schema.Index方法的具體用法?Python schema.Index怎麽用?Python schema.Index使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sqlalchemy.schema
的用法示例。
在下文中一共展示了schema.Index方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_noindexes_table
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def test_noindexes_table(metadata):
simple_items = Table("simple_items", metadata, Column("number", INTEGER), CheckConstraint("number > 2"))
simple_items.indexes.add(Index("idx_number", simple_items.c.number))
assert (
generate_code(metadata, noindexes=True)
== """\
# coding: utf-8
from sqlalchemy import CheckConstraint, Column, Integer, MetaData, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('number', Integer),
CheckConstraint('number > 2')
)
"""
)
示例2: test_noconstraints_table
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def test_noconstraints_table(metadata):
simple_items = Table("simple_items", metadata, Column("number", INTEGER), CheckConstraint("number > 2"))
simple_items.indexes.add(Index("idx_number", simple_items.c.number))
assert (
generate_code(metadata, noconstraints=True)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer, MetaData, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('number', Integer, index=True)
)
"""
)
示例3: test_indexes_table
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def test_indexes_table(metadata):
simple_items = Table("simple_items", metadata, Column("id", INTEGER), Column("number", INTEGER), Column("text", VARCHAR))
simple_items.indexes.add(Index("idx_number", simple_items.c.number))
simple_items.indexes.add(Index("idx_text_number", simple_items.c.text, simple_items.c.number, unique=True))
simple_items.indexes.add(Index("idx_text", simple_items.c.text, unique=True))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Index, Integer, MetaData, String, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('id', Integer),
Column('number', Integer, index=True),
Column('text', String, unique=True),
Index('idx_text_number', 'text', 'number', unique=True)
)
"""
)
示例4: test_include_columns_indexes
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def test_include_columns_indexes(self):
m = self.metadata
t1 = Table("t1", m, Column("a", sa.Integer), Column("b", sa.Integer))
sa.Index("foobar", t1.c.a, t1.c.b)
sa.Index("bat", t1.c.a)
m.create_all()
m2 = MetaData(testing.db)
t2 = Table("t1", m2, autoload=True)
assert len(t2.indexes) == 2
m2 = MetaData(testing.db)
t2 = Table("t1", m2, autoload=True, include_columns=["a"])
assert len(t2.indexes) == 1
m2 = MetaData(testing.db)
t2 = Table("t1", m2, autoload=True, include_columns=["a", "b"])
assert len(t2.indexes) == 2
示例5: test_index_reflection_cols_busted
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def test_index_reflection_cols_busted(self):
t = Table(
"x", self.metadata, Column("a", Integer), Column("b", Integer)
)
sa.Index("x_ix", t.c.a, t.c.b)
self.metadata.create_all()
def mock_get_columns(self, connection, table_name, **kw):
return [{"name": "b", "type": Integer, "primary_key": False}]
with testing.mock.patch.object(
testing.db.dialect, "get_columns", mock_get_columns
):
m = MetaData()
with testing.expect_warnings(
"index key 'a' was not located in columns"
):
t = Table("x", m, autoload=True, autoload_with=testing.db)
eq_(list(t.indexes)[0].columns, [t.c.b])
示例6: test_non_attached_col_plus_string_expr
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def test_non_attached_col_plus_string_expr(self):
# another one that declarative can lead towards
metadata = MetaData()
t1 = Table("a", metadata, Column("id", Integer))
c2 = Column("x", Integer)
# if we do it here, no problem
# t1.append_column(c2)
idx = Index("foo", c2, desc("foo"))
t1.append_column(c2)
self._assert_index_col_x(t1, idx, columns=True)
示例7: test_auto_append_constraint
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def test_auto_append_constraint(self):
m = MetaData()
t = Table("tbl", m, Column("a", Integer), Column("b", Integer))
t2 = Table("t2", m, Column("a", Integer), Column("b", Integer))
for c in (
UniqueConstraint(t.c.a),
CheckConstraint(t.c.a > 5),
ForeignKeyConstraint([t.c.a], [t2.c.a]),
PrimaryKeyConstraint(t.c.a),
):
assert c in t.constraints
t.append_constraint(c)
assert c in t.constraints
c = Index("foo", t.c.a)
assert c in t.indexes
示例8: test_raise_clauseelement_not_a_column
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def test_raise_clauseelement_not_a_column(self):
m = MetaData()
t2 = Table("t2", m, Column("x", Integer))
class SomeClass(object):
def __clause_element__(self):
return t2
assert_raises_message(
exc.ArgumentError,
r"String column name or column expression for DDL constraint "
r"expected, got .*SomeClass",
Index,
"foo",
SomeClass(),
)
示例9: test_combined
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def test_combined(self):
with self._fixture():
idx = Index(
"a", "b", "c", participating_x=7, nonparticipating_y=True
)
eq_(
idx.dialect_options,
{
"participating": {"y": False, "x": 7, "z_one": None},
"nonparticipating": {"y": True, "*": None},
},
)
eq_(
idx.dialect_kwargs,
{"participating_x": 7, "nonparticipating_y": True},
)
示例10: test_multiple_participating
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def test_multiple_participating(self):
with self._fixture():
idx = Index(
"a",
"b",
"c",
participating_x=7,
participating2_x=15,
participating2_y="lazy",
)
eq_(
idx.dialect_options,
{
"participating": {"x": 7, "y": False, "z_one": None},
"participating2": {"x": 15, "y": "lazy", "pp": "default"},
},
)
eq_(
idx.dialect_kwargs,
{
"participating_x": 7,
"participating2_x": 15,
"participating2_y": "lazy",
},
)
示例11: create_index
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def create_index(self, columns, name=None, **kw):
"""Create an index to speed up queries on a table.
If no ``name`` is given a random name is created.
::
table.create_index(['name', 'country'])
"""
columns = [normalize_column_name(c) for c in ensure_tuple(columns)]
with self.db.lock:
if not self.exists:
raise DatasetException("Table has not been created yet.")
for column in columns:
if not self.has_column(column):
return
if not self.has_index(columns):
self._threading_warn()
name = name or index_name(self.name, columns)
columns = [self.table.c[c] for c in columns]
idx = Index(name, *columns, **kw)
idx.create(self.db.executable)
示例12: _get_index_final_name
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def _get_index_final_name(dialect, idx):
# trying to keep the truncation rules totally localized on the
# SQLA side while also stepping around the quoting issue. Ideally
# the _prepared_index_name() method on the SQLA side would have
# a quoting option or the truncation routine would be broken out.
#
# test for SQLA quoted_name construct, introduced in
# 0.9 or thereabouts.
# this doesn't work in 0.8 and the "quote" option on Index doesn't
# seem to work in 0.8 either.
if hasattr(idx.name, "quote"):
# might be quoted_name, might be truncated_name, keep it the
# same
quoted_name_cls = type(idx.name)
new_name = quoted_name_cls(str(idx.name), quote=False)
idx = schema.Index(name=new_name)
return dialect.ddl_compiler(dialect, None)._prepared_index_name(idx)
示例13: index
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def index(self, name, tablename, columns, schema=None, **kw):
t = sa_schema.Table(
tablename or 'no_table', self.metadata(),
schema=schema
)
idx = sa_schema.Index(
name,
*[util.sqla_compat._textual_index_column(t, n) for n in columns],
**kw)
return idx
示例14: _make_index
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def _make_index(params, conn_table):
# TODO: add .info such as 'duplicates_constraint'
return sa_schema.Index(
params['name'],
*[conn_table.c[cname] for cname in params['column_names']],
unique=params['unique']
)
示例15: upgrade
# 需要導入模塊: from sqlalchemy import schema [as 別名]
# 或者: from sqlalchemy.schema import Index [as 別名]
def upgrade(migrate_engine):
LOG.info("Adding boolean column delayed_notify to table 'zones'")
meta.bind = migrate_engine
zones_table = Table('zones', meta, autoload=True)
col = Column('delayed_notify', Boolean(), default=False)
col.create(zones_table)
index = Index('delayed_notify', zones_table.c.delayed_notify)
index.create(migrate_engine)