本文整理汇总了Python中sqlalchemy.schema.ForeignKey方法的典型用法代码示例。如果您正苦于以下问题:Python schema.ForeignKey方法的具体用法?Python schema.ForeignKey怎么用?Python schema.ForeignKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.schema
的用法示例。
在下文中一共展示了schema.ForeignKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_fk_mapper
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def get_fk_mapper(self, registry):
"""Return the foreign key which represent the attribute in the data
base
:param registry: instance of the sqlalchemy ForeignKey
:rtype: instance of the attribute
"""
Model = self.check_model_in_first_step(registry)
try:
column_name = self.check_column_in_first_step(registry, Model)
if Model[column_name].foreign_key:
return Model[column_name].foreign_key
except ModelAttributeException:
pass
return None
示例2: test_foreign_key_options
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def test_foreign_key_options(metadata):
Table(
"simple_items",
metadata,
Column(
"name", VARCHAR, ForeignKey("simple_items.name", ondelete="CASCADE", onupdate="CASCADE", deferrable=True, initially="DEFERRED")
),
)
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, ForeignKey, MetaData, String, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('name', String, ForeignKey('simple_items.name', ondelete='CASCADE', \
onupdate='CASCADE', deferrable=True, initially='DEFERRED'))
)
"""
)
示例3: render_constraint
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def render_constraint(self, constraint):
def render_fk_options(*opts):
opts = [repr(opt) for opt in opts]
for attr in "ondelete", "onupdate", "deferrable", "initially", "match":
value = getattr(constraint, attr, None)
if value:
opts.append("{0}={1!r}".format(attr, value))
return ", ".join(opts)
if isinstance(constraint, ForeignKey):
remote_column = "{0}.{1}".format(constraint.column.table.fullname, constraint.column.name)
return "ForeignKey({0})".format(render_fk_options(remote_column))
elif isinstance(constraint, ForeignKeyConstraint):
local_columns = _get_column_names(constraint)
remote_columns = ["{0}.{1}".format(fk.column.table.fullname, fk.column.name) for fk in constraint.elements]
return "ForeignKeyConstraint({0})".format(render_fk_options(local_columns, remote_columns))
elif isinstance(constraint, CheckConstraint):
return "CheckConstraint({0!r})".format(self._get_compiled_expression(constraint.sqltext))
elif isinstance(constraint, UniqueConstraint):
columns = [repr(col.name) for col in constraint.columns]
return "UniqueConstraint({0})".format(", ".join(columns))
示例4: test_get_fk
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def test_get_fk(self, registry_blok):
registry = registry_blok
ma = ModelAttribute('Model.System.Model', 'name')
assert isinstance(ma.get_fk(registry), ForeignKey)
示例5: test_get_fk_with_options
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def test_get_fk_with_options(self, registry_blok):
registry = registry_blok
ma = ModelAttribute('Model.System.Model', 'name').options(
ondelete='cascade')
mafk = ma.get_fk(registry)
assert isinstance(mafk, ForeignKey)
示例6: get_fk_column
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def get_fk_column(self, registry):
"""Return the foreign key which represent the attribute in the data
base
:param registry: instance of the sqlalchemy ForeignKey
:rtype: instance of the attribute
"""
mapper = self.get_fk_mapper(registry)
if mapper:
return mapper.attribute_name
return None
示例7: get_fk
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def get_fk(self, registry):
"""Return the foreign key which represent the attribute in the data
base
:param registry: instance of the sqlalchemy ForeignKey
:rtype: instance of the attribute
"""
return ForeignKey(self.get_fk_name(registry), **self._options)
示例8: test_onetomany
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def test_onetomany(metadata):
Table(
"simple_items",
metadata,
Column("id", INTEGER, primary_key=True),
Column("container_id", INTEGER),
ForeignKeyConstraint(["container_id"], ["simple_containers.id"]),
)
Table("simple_containers", metadata, Column("id", INTEGER, primary_key=True))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class SimpleContainer(Base):
__tablename__ = 'simple_containers'
id = Column(Integer, primary_key=True)
class SimpleItem(Base):
__tablename__ = 'simple_items'
id = Column(Integer, primary_key=True)
container_id = Column(ForeignKey('simple_containers.id'))
container = relationship('SimpleContainer')
"""
)
示例9: test_onetomany_selfref
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def test_onetomany_selfref(metadata):
Table(
"simple_items",
metadata,
Column("id", INTEGER, primary_key=True),
Column("parent_item_id", INTEGER),
ForeignKeyConstraint(["parent_item_id"], ["simple_items.id"]),
)
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class SimpleItem(Base):
__tablename__ = 'simple_items'
id = Column(Integer, primary_key=True)
parent_item_id = Column(ForeignKey('simple_items.id'))
parent_item = relationship('SimpleItem', remote_side=[id])
"""
)
示例10: test_onetomany_selfref_multi
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def test_onetomany_selfref_multi(metadata):
Table(
"simple_items",
metadata,
Column("id", INTEGER, primary_key=True),
Column("parent_item_id", INTEGER),
Column("top_item_id", INTEGER),
ForeignKeyConstraint(["parent_item_id"], ["simple_items.id"]),
ForeignKeyConstraint(["top_item_id"], ["simple_items.id"]),
)
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class SimpleItem(Base):
__tablename__ = 'simple_items'
id = Column(Integer, primary_key=True)
parent_item_id = Column(ForeignKey('simple_items.id'))
top_item_id = Column(ForeignKey('simple_items.id'))
parent_item = relationship('SimpleItem', remote_side=[id], \
primaryjoin='SimpleItem.parent_item_id == SimpleItem.id')
top_item = relationship('SimpleItem', remote_side=[id], \
primaryjoin='SimpleItem.top_item_id == SimpleItem.id')
"""
)
示例11: test_onetoone
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def test_onetoone(metadata):
Table(
"simple_items",
metadata,
Column("id", INTEGER, primary_key=True),
Column("other_item_id", INTEGER),
ForeignKeyConstraint(["other_item_id"], ["other_items.id"]),
UniqueConstraint("other_item_id"),
)
Table("other_items", metadata, Column("id", INTEGER, primary_key=True))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class OtherItem(Base):
__tablename__ = 'other_items'
id = Column(Integer, primary_key=True)
class SimpleItem(Base):
__tablename__ = 'simple_items'
id = Column(Integer, primary_key=True)
other_item_id = Column(ForeignKey('other_items.id'), unique=True)
other_item = relationship('OtherItem', uselist=False)
"""
)
示例12: test_foreign_key_schema
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def test_foreign_key_schema(metadata):
Table(
"simple_items",
metadata,
Column("id", INTEGER, primary_key=True),
Column("other_item_id", INTEGER),
ForeignKeyConstraint(["other_item_id"], ["otherschema.other_items.id"]),
)
Table("other_items", metadata, Column("id", INTEGER, primary_key=True), schema="otherschema")
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class OtherItem(Base):
__tablename__ = 'other_items'
__table_args__ = {'schema': 'otherschema'}
id = Column(Integer, primary_key=True)
class SimpleItem(Base):
__tablename__ = 'simple_items'
id = Column(Integer, primary_key=True)
other_item_id = Column(ForeignKey('otherschema.other_items.id'))
other_item = relationship('OtherItem')
"""
)
示例13: setUp
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def setUp(self):
super(DropAllObjectsTest, self).setUp()
self.metadata = metadata = schema.MetaData()
schema.Table(
'a', metadata,
schema.Column('id', types.Integer, primary_key=True),
mysql_engine='InnoDB'
)
schema.Table(
'b', metadata,
schema.Column('id', types.Integer, primary_key=True),
schema.Column('a_id', types.Integer, schema.ForeignKey('a.id')),
mysql_engine='InnoDB'
)
schema.Table(
'c', metadata,
schema.Column('id', types.Integer, primary_key=True),
schema.Column('b_id', types.Integer, schema.ForeignKey('b.id')),
schema.Column(
'd_id', types.Integer,
schema.ForeignKey('d.id', use_alter=True, name='c_d_fk')),
mysql_engine='InnoDB'
)
schema.Table(
'd', metadata,
schema.Column('id', types.Integer, primary_key=True),
schema.Column('c_id', types.Integer, schema.ForeignKey('c.id')),
mysql_engine='InnoDB'
)
metadata.create_all(self.engine, checkfirst=False)
# will drop nothing if the test worked
self.addCleanup(metadata.drop_all, self.engine, checkfirst=True)
示例14: user_address_fixture
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def user_address_fixture(self, metadata):
users = Table(
"users",
metadata,
Column("id", Integer, primary_key=True),
Column("name", String(50)),
)
addresses = Table(
"addresses",
metadata,
Column("id", Integer),
Column("user_id", ForeignKey("users.id")),
Column("email", String(50)),
)
return users, addresses
示例15: test_onetomany_multiref
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import ForeignKey [as 别名]
def test_onetomany_multiref(metadata):
Table(
"simple_items",
metadata,
Column("id", INTEGER, primary_key=True),
Column("parent_container_id", INTEGER),
Column("top_container_id", INTEGER),
ForeignKeyConstraint(["parent_container_id"], ["simple_containers.id"]),
ForeignKeyConstraint(["top_container_id"], ["simple_containers.id"]),
)
Table("simple_containers", metadata, Column("id", INTEGER, primary_key=True))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class SimpleContainer(Base):
__tablename__ = 'simple_containers'
id = Column(Integer, primary_key=True)
class SimpleItem(Base):
__tablename__ = 'simple_items'
id = Column(Integer, primary_key=True)
parent_container_id = Column(ForeignKey('simple_containers.id'))
top_container_id = Column(ForeignKey('simple_containers.id'))
parent_container = relationship('SimpleContainer', \
primaryjoin='SimpleItem.parent_container_id == SimpleContainer.id')
top_container = relationship('SimpleContainer', \
primaryjoin='SimpleItem.top_container_id == SimpleContainer.id')
"""
)