本文整理汇总了Python中sqlalchemy.schema.CheckConstraint方法的典型用法代码示例。如果您正苦于以下问题:Python schema.CheckConstraint方法的具体用法?Python schema.CheckConstraint怎么用?Python schema.CheckConstraint使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.schema
的用法示例。
在下文中一共展示了schema.CheckConstraint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generic_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def generic_constraint(self, name, table_name, type_, schema=None, **kw):
t = self.table(table_name, schema=schema)
types = {
'foreignkey': lambda name: sa_schema.ForeignKeyConstraint(
[], [], name=name),
'primary': sa_schema.PrimaryKeyConstraint,
'unique': sa_schema.UniqueConstraint,
'check': lambda name: sa_schema.CheckConstraint("", name=name),
None: sa_schema.Constraint
}
try:
const = types[type_]
except KeyError:
raise TypeError("'type' can be one of %s" %
", ".join(sorted(repr(x) for x in types)))
else:
const = const(name=name)
t.append_constraint(const)
return const
示例2: _mysql_drop_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def _mysql_drop_constraint(element, compiler, **kw):
"""Redefine SQLAlchemy's drop constraint to
raise errors for invalid constraint type."""
constraint = element.element
if isinstance(constraint, (schema.ForeignKeyConstraint,
schema.PrimaryKeyConstraint,
schema.UniqueConstraint)
):
return compiler.visit_drop_constraint(element, **kw)
elif isinstance(constraint, schema.CheckConstraint):
raise NotImplementedError(
"MySQL does not support CHECK constraints.")
else:
raise NotImplementedError(
"No generic 'DROP CONSTRAINT' in MySQL - "
"please specify constraint type")
示例3: __table_args__
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def __table_args__(cls): # pylint: disable=no-self-argument
# exception for CERN Oracle identifier length limitations
# pylint: disable=maybe-no-member
if cls.__tablename__.upper() == 'UPDATED_ACCOUNT_COUNTERS':
return cls._table_args + (CheckConstraint('CREATED_AT IS NOT NULL', 'UPDATED_ACCNT_CNTRS_CREATED_NN'),
CheckConstraint('UPDATED_AT IS NOT NULL', 'UPDATED_ACCNT_CNTRS_UPDATED_NN'),
{'mysql_engine': 'InnoDB'})
# pylint: disable=maybe-no-member
elif cls.__tablename__.upper() == 'UPDATED_RSE_COUNTERS':
return cls._table_args + (CheckConstraint('CREATED_AT IS NOT NULL', 'UPDATED_RSE_CNTRS_CREATED_NN'),
CheckConstraint('UPDATED_AT IS NOT NULL', 'UPDATED_RSE_CNTRS_UPDATED_NN'),
{'mysql_engine': 'InnoDB'})
# pylint: disable=maybe-no-member
elif cls.__tablename__.upper() == 'DIDS_FOLLOWED_EVENTS':
return cls._table_args + (CheckConstraint('CREATED_AT IS NOT NULL', 'DIDS_FOLLOWED_EVENTS_CRE_NN'),
CheckConstraint('UPDATED_AT IS NOT NULL', 'DIDS_FOLLOWED_EVENTS_UPD_NN'),
{'mysql_engine': 'InnoDB'})
# otherwise, proceed normally
# pylint: disable=maybe-no-member
return cls._table_args + (CheckConstraint('CREATED_AT IS NOT NULL', name=cls.__tablename__.upper() + '_CREATED_NN'),
CheckConstraint('UPDATED_AT IS NOT NULL', name=cls.__tablename__.upper() + '_UPDATED_NN'),
{'mysql_engine': 'InnoDB'})
示例4: test_enum_detection
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def test_enum_detection(metadata):
Table("simple_items", metadata, Column("enum", VARCHAR(255)), CheckConstraint(r"simple_items.enum IN ('A', '\'B', 'C')"))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Enum, MetaData, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('enum', Enum('A', "\\\\'B", 'C'))
)
"""
)
示例5: test_noindexes_table
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [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')
)
"""
)
示例6: test_schema_boolean
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def test_schema_boolean(metadata):
Table(
"simple_items", metadata, Column("bool1", INTEGER), CheckConstraint("testschema.simple_items.bool1 IN (0, 1)"), schema="testschema"
)
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Boolean, Column, MetaData, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('bool1', Boolean),
schema='testschema'
)
"""
)
示例7: generic_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def generic_constraint(self, name, table_name, type_, schema=None, **kw):
t = self.table(table_name, schema=schema)
types = {
"foreignkey": lambda name: sa_schema.ForeignKeyConstraint(
[], [], name=name
),
"primary": sa_schema.PrimaryKeyConstraint,
"unique": sa_schema.UniqueConstraint,
"check": lambda name: sa_schema.CheckConstraint("", name=name),
None: sa_schema.Constraint,
}
try:
const = types[type_]
except KeyError:
raise TypeError(
"'type' can be one of %s"
% ", ".join(sorted(repr(x) for x in types))
)
else:
const = const(name=name)
t.append_constraint(const)
return const
示例8: test_change_name_change_metadata
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def test_change_name_change_metadata(self):
meta = MetaData()
meta2 = MetaData()
table = Table(
"mytable",
meta,
Column("myid", Integer, primary_key=True),
Column("name", String(40), nullable=True),
Column(
"description", String(30), CheckConstraint("description='hi'")
),
UniqueConstraint("name"),
schema="myschema",
)
table2 = table.to_metadata(meta2, name="newtable")
assert table.metadata is not table2.metadata
eq_((table.name, table2.name), ("mytable", "newtable"))
eq_((table.key, table2.key), ("myschema.mytable", "myschema.newtable"))
示例9: test_enum_constraint_type_doesnt_double
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def test_enum_constraint_type_doesnt_double(self):
m1 = MetaData()
t1 = Table(
"x",
m1,
Column("flag", Enum("a", "b", "c", create_constraint=True)),
)
eq_(
len([c for c in t1.constraints if isinstance(c, CheckConstraint)]),
1,
)
m2 = MetaData()
t2 = t1.to_metadata(m2)
eq_(
len([c for c in t2.constraints if isinstance(c, CheckConstraint)]),
1,
)
示例10: test_to_metadata_ok
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def test_to_metadata_ok(self):
m = MetaData()
t = Table("tbl", m, Column("a", Integer), Column("b", Integer))
t2 = Table("t2", m, Column("a", Integer), Column("b", Integer))
UniqueConstraint(t.c.a)
CheckConstraint(t.c.a > 5)
ForeignKeyConstraint([t.c.a], [t2.c.a])
PrimaryKeyConstraint(t.c.a)
m2 = MetaData()
t3 = t.to_metadata(m2)
eq_(len(t3.constraints), 4)
for c in t3.constraints:
assert c.table is t3
示例11: test_auto_append_ck_on_col_attach_three
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def test_auto_append_ck_on_col_attach_three(self):
m = MetaData()
a = Column("a", Integer)
b = Column("b", Integer)
c = Column("c", Integer)
ck = CheckConstraint(a > b + c)
t = Table("tbl", m, a)
assert ck not in t.constraints
t.append_column(b)
assert ck not in t.constraints
t2 = Table("t2", m)
t2.append_column(c)
# two different tables, so CheckConstraint does nothing.
assert ck not in t.constraints
示例12: test_schematype_ck_name_boolean
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def test_schematype_ck_name_boolean(self):
m1 = MetaData(
naming_convention={"ck": "ck_%(table_name)s_%(constraint_name)s"}
)
u1 = Table(
"user",
m1,
Column("x", Boolean(name="foo", create_constraint=True)),
)
# constraint is not hit
eq_(
[c for c in u1.constraints if isinstance(c, CheckConstraint)][
0
].name,
"foo",
)
# but is hit at compile time
self.assert_compile(
schema.CreateTable(u1),
'CREATE TABLE "user" ('
"x BOOLEAN, "
"CONSTRAINT ck_user_foo CHECK (x IN (0, 1))"
")",
)
示例13: test_schematype_ck_name_boolean_not_on_name
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def test_schematype_ck_name_boolean_not_on_name(self):
m1 = MetaData(
naming_convention={"ck": "ck_%(table_name)s_%(column_0_name)s"}
)
u1 = Table("user", m1, Column("x", Boolean(create_constraint=True)))
# constraint is not hit
is_(
[c for c in u1.constraints if isinstance(c, CheckConstraint)][
0
].name,
_NONE_NAME,
)
# but is hit at compile time
self.assert_compile(
schema.CreateTable(u1),
'CREATE TABLE "user" ('
"x BOOLEAN, "
"CONSTRAINT ck_user_x CHECK (x IN (0, 1))"
")",
)
示例14: test_schematype_ck_name_enum
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def test_schematype_ck_name_enum(self):
m1 = MetaData(
naming_convention={"ck": "ck_%(table_name)s_%(constraint_name)s"}
)
u1 = Table(
"user",
m1,
Column("x", Enum("a", "b", name="foo", create_constraint=True)),
)
eq_(
[c for c in u1.constraints if isinstance(c, CheckConstraint)][
0
].name,
"foo",
)
# but is hit at compile time
self.assert_compile(
schema.CreateTable(u1),
'CREATE TABLE "user" ('
"x VARCHAR(1), "
"CONSTRAINT ck_user_foo CHECK (x IN ('a', 'b'))"
")",
)
示例15: _mysql_drop_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import CheckConstraint [as 别名]
def _mysql_drop_constraint(element, compiler, **kw):
"""Redefine SQLAlchemy's drop constraint to
raise errors for invalid constraint type."""
constraint = element.element
if isinstance(constraint, (schema.ForeignKeyConstraint,
schema.PrimaryKeyConstraint,
schema.UniqueConstraint)
):
return compiler.visit_drop_constraint(element, **kw)
elif isinstance(constraint, schema.CheckConstraint):
# note that SQLAlchemy as of 1.2 does not yet support
# DROP CONSTRAINT for MySQL/MariaDB, so we implement fully
# here.
return "ALTER TABLE %s DROP CONSTRAINT %s" % \
(compiler.preparer.format_table(constraint.table),
compiler.preparer.format_constraint(constraint))
else:
raise NotImplementedError(
"No generic 'DROP CONSTRAINT' in MySQL - "
"please specify constraint type")