本文整理汇总了Python中sqlalchemy.schema.PrimaryKeyConstraint方法的典型用法代码示例。如果您正苦于以下问题:Python schema.PrimaryKeyConstraint方法的具体用法?Python schema.PrimaryKeyConstraint怎么用?Python schema.PrimaryKeyConstraint使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.schema
的用法示例。
在下文中一共展示了schema.PrimaryKeyConstraint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_column
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [as 别名]
def add_column(operations, operation):
table_name = operation.table_name
column = operation.column
schema = operation.schema
t = operations.schema_obj.table(table_name, column, schema=schema)
operations.impl.add_column(
table_name,
column,
schema=schema
)
for constraint in t.constraints:
if not isinstance(constraint, sa_schema.PrimaryKeyConstraint):
operations.impl.add_constraint(constraint)
for index in t.indexes:
operations.impl.create_index(index)
示例2: generic_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [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
示例3: add_column
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [as 别名]
def add_column(operations, operation):
table_name = operation.table_name
column = operation.column
schema = operation.schema
kw = operation.kw
t = operations.schema_obj.table(table_name, column, schema=schema)
operations.impl.add_column(table_name, column, schema=schema, **kw)
for constraint in t.constraints:
if not isinstance(constraint, sa_schema.PrimaryKeyConstraint):
operations.impl.add_constraint(constraint)
for index in t.indexes:
operations.impl.create_index(index)
with_comment = (
sqla_compat._dialect_supports_comments(operations.impl.dialect)
and not operations.impl.dialect.inline_comments
)
comment = sqla_compat._comment_attribute(column)
if comment and with_comment:
operations.impl.create_column_comment(column)
示例4: drop_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [as 别名]
def drop_constraint(self, const):
if not const.name:
raise ValueError("Constraint must have a name")
try:
const = self.named_constraints.pop(const.name)
except KeyError:
if _is_type_bound(const):
# type-bound constraints are only included in the new
# table via their type object in any case, so ignore the
# drop_constraint() that comes here via the
# Operations.implementation_for(alter_column)
return
raise ValueError("No such constraint: '%s'" % const.name)
else:
if isinstance(const, PrimaryKeyConstraint):
for col in const.columns:
self.columns[col.name].primary_key = False
示例5: test_composite_pk_constraint_maintains_order_explicit
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [as 别名]
def test_composite_pk_constraint_maintains_order_explicit(self):
m = MetaData()
t = Table(
"t",
m,
Column("a", Integer),
Column("b", Integer, autoincrement=True),
schema.PrimaryKeyConstraint("a", "b"),
)
self.assert_compile(
schema.CreateTable(t),
"CREATE TABLE t ("
"a INTEGER NOT NULL, "
"b INTEGER NOT NULL, "
"PRIMARY KEY (a, b))",
)
示例6: test_pk_always_flips_nullable
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [as 别名]
def test_pk_always_flips_nullable(self):
m = MetaData()
t1 = Table("t1", m, Column("x", Integer), PrimaryKeyConstraint("x"))
t2 = Table("t2", m, Column("x", Integer, primary_key=True))
eq_(list(t1.primary_key), [t1.c.x])
eq_(list(t2.primary_key), [t2.c.x])
assert t1.c.x.primary_key
assert t2.c.x.primary_key
assert not t2.c.x.nullable
assert not t1.c.x.nullable
示例7: test_auto_append_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [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_to_metadata_ok
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [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
示例9: test_table_cls_attribute_return_none
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [as 别名]
def test_table_cls_attribute_return_none(self):
from sqlalchemy.schema import Column, PrimaryKeyConstraint
class AutoTable(object):
@declared_attr
def __tablename__(cls):
return cls.__name__
@classmethod
def __table_cls__(cls, *arg, **kw):
for obj in arg[1:]:
if (
isinstance(obj, Column) and obj.primary_key
) or isinstance(obj, PrimaryKeyConstraint):
return Table(*arg, **kw)
return None
class Person(AutoTable, Base):
id = Column(Integer, primary_key=True)
class Employee(Person):
employee_name = Column(String)
is_(inspect(Employee).local_table, Person.__table__)
示例10: _mysql_drop_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [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")
示例11: add_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [as 别名]
def add_constraint(self, const):
if not const.name:
raise ValueError("Constraint must have a name")
if isinstance(const, sql_schema.PrimaryKeyConstraint):
if self.table.primary_key in self.unnamed_constraints:
self.unnamed_constraints.remove(self.table.primary_key)
self.named_constraints[const.name] = const
示例12: primary_key_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [as 别名]
def primary_key_constraint(self, name, table_name, cols, schema=None):
m = self.metadata()
columns = [sa_schema.Column(n, NULLTYPE) for n in cols]
t = sa_schema.Table(
table_name, m,
*columns,
schema=schema)
p = sa_schema.PrimaryKeyConstraint(
*[t.c[n] for n in cols], name=name)
t.append_constraint(p)
return p
示例13: primary_key_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [as 别名]
def primary_key_constraint(self, name, table_name, cols, schema=None):
m = self.metadata()
columns = [sa_schema.Column(n, NULLTYPE) for n in cols]
t = sa_schema.Table(table_name, m, *columns, schema=schema)
p = sa_schema.PrimaryKeyConstraint(*[t.c[n] for n in cols], name=name)
t.append_constraint(p)
return p
示例14: _mysql_drop_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [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.
if _is_mariadb(compiler.dialect):
return "ALTER TABLE %s DROP CONSTRAINT %s" % (
compiler.preparer.format_table(constraint.table),
compiler.preparer.format_constraint(constraint),
)
else:
return "ALTER TABLE %s DROP CHECK %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"
)
示例15: visit_column
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import PrimaryKeyConstraint [as 别名]
def visit_column(self, column):
"""Firebird supports 'DROP col' instead of 'DROP COLUMN col' syntax
Drop primary key and unique constraints if dropped column is referencing it."""
if column.primary_key:
if column.table.primary_key.columns.contains_column(column):
column.table.primary_key.drop()
# TODO: recreate primary key if it references more than this column
for index in column.table.indexes:
# "column in index.columns" causes problems as all
# column objects compare equal and return a SQL expression
if column.name in [col.name for col in index.columns]:
index.drop()
# TODO: recreate index if it references more than this column
for cons in column.table.constraints:
if isinstance(cons,PrimaryKeyConstraint):
# will be deleted only when the column its on
# is deleted!
continue
should_drop = column.name in cons.columns
if should_drop:
self.start_alter_table(column)
self.append("DROP CONSTRAINT ")
self.append(self.preparer.format_constraint(cons))
self.execute()
# TODO: recreate unique constraint if it refenrences more than this column
self.start_alter_table(column)
self.append('DROP %s' % self.preparer.format_column(column))
self.execute()