本文整理汇总了Python中sqlalchemy.Sequence方法的典型用法代码示例。如果您正苦于以下问题:Python sqlalchemy.Sequence方法的具体用法?Python sqlalchemy.Sequence怎么用?Python sqlalchemy.Sequence使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy
的用法示例。
在下文中一共展示了sqlalchemy.Sequence方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: initialize_model
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def initialize_model(cls):
""" Create the sequence to determine name """
super(Sequence, cls).initialize_model()
seq = SQLASequence(cls._cls_seq_name)
seq.create(cls.registry.bind)
to_create = getattr(cls.registry,
'_need_sequence_to_create_if_not_exist', ())
if to_create is None:
return
for vals in to_create:
if cls.query().filter(cls.code == vals['code']).count():
continue
formatter = vals.get('formater')
if formatter is None:
del vals['formater']
cls.insert(**vals)
示例2: create_sequence
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def create_sequence(cls, values):
"""Create the database sequence for an instance of Sequence Model.
:return: suitable field values for insertion of the Model instance
:rtype: dict
"""
seq_name = values.get('seq_name')
if seq_name is None:
seq_id = cls.registry.execute(SQLASequence(cls._cls_seq_name))
seq_name = '%s_%d' % (cls.__tablename__, seq_id)
values['seq_name'] = seq_name
number = values.setdefault('number', 0)
if number:
seq = SQLASequence(seq_name, number)
else:
seq = SQLASequence(seq_name)
seq.create(cls.registry.bind)
return values
示例3: generate_ori_identifier
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def generate_ori_identifier(self, iri):
"""
Generates a Resource with an ORI identifier and adds the IRI as a Source if it does not already exist.
"""
session = self.Session()
new_id = self.engine.execute(Sequence('ori_id_seq'))
new_identifier = Uri(Ori, new_id)
try:
# If the resource already exists, create the source as a child of the resource
resource = session.query(Source).filter(Source.iri == iri).one().resource
resource.sources.append(Source(iri=iri))
session.flush()
except NoResultFound:
# If the resource does not exist, create resource and source together
resource = Resource(ori_id=new_id, iri=new_identifier, sources=[Source(iri=iri)])
session.add(resource)
session.commit()
finally:
session.close()
return new_identifier
示例4: _create_users_addresses_tables
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def _create_users_addresses_tables(engine_testaccount, metadata):
users = Table('users', metadata,
Column('id', Integer, Sequence('user_id_seq'),
primary_key=True),
Column('name', String),
Column('fullname', String),
)
addresses = Table('addresses', metadata,
Column('id', Integer, Sequence('address_id_seq'),
primary_key=True),
Column('user_id', None, ForeignKey('users.id')),
Column('email_address', String, nullable=False)
)
metadata.create_all(engine_testaccount)
return users, addresses
示例5: _create_sequence
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def _create_sequence(table_name: Text) -> "Sequence":
"""Creates a sequence object for a specific table name.
If using Oracle you will need to create a sequence in your database,
as described here: https://rasa.com/docs/rasa/api/tracker-stores/#sqltrackerstore
Args:
table_name: The name of the table, which gets a Sequence assigned
Returns: A `Sequence` object
"""
from sqlalchemy.ext.declarative import declarative_base
sequence_name = f"{table_name}_seq"
Base = declarative_base()
return sa.Sequence(sequence_name, metadata=Base.metadata, optional=True)
示例6: test_sequence_not_duped
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def test_sequence_not_duped(self):
engine, buf = self._engine_fixture()
metadata = MetaData()
t = Table(
"testtable",
metadata,
Column(
"pk", Integer, Sequence("testtable_pk_seq"), primary_key=True,
),
)
t.create(engine)
t.drop(engine)
eq_(re.findall(r"CREATE (\w+)", buf.getvalue()), ["SEQUENCE", "TABLE"])
eq_(re.findall(r"DROP (\w+)", buf.getvalue()), ["TABLE", "SEQUENCE"])
示例7: test_strlen
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def test_strlen(self):
metadata = self.metadata
# On FB the length() function is implemented by an external UDF,
# strlen(). Various SA tests fail because they pass a parameter
# to it, and that does not work (it always results the maximum
# string length the UDF was declared to accept). This test
# checks that at least it works ok in other cases.
t = Table(
"t1",
metadata,
Column("id", Integer, Sequence("t1idseq"), primary_key=True),
Column("name", String(10)),
)
metadata.create_all()
t.insert(values=dict(name="dante")).execute()
t.insert(values=dict(name="alighieri")).execute()
select(
[func.count(t.c.id)], func.length(t.c.name) == 5
).execute().first()[0] == 1
示例8: test_format
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def test_format(self):
seq = Sequence("my_seq_no_schema")
dialect = postgresql.dialect()
assert (
dialect.identifier_preparer.format_sequence(seq)
== "my_seq_no_schema"
)
seq = Sequence("my_seq", schema="some_schema")
assert (
dialect.identifier_preparer.format_sequence(seq)
== "some_schema.my_seq"
)
seq = Sequence("My_Seq", schema="Some_Schema")
assert (
dialect.identifier_preparer.format_sequence(seq)
== '"Some_Schema"."My_Seq"'
)
示例9: test_insert_from_select_seq
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def test_insert_from_select_seq(self):
m = MetaData()
t1 = Table(
"t",
m,
Column("id", Integer, Sequence("id_seq"), primary_key=True),
Column("data", String),
)
stmt = t1.insert().from_select(("data",), select([t1.c.data]))
self.assert_compile(
stmt,
"INSERT INTO t (data, id) SELECT t.data, "
"nextval('id_seq') AS next_value_1 FROM t",
dialect=postgresql.dialect(),
)
示例10: dataset_no_autoinc
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def dataset_no_autoinc(self, metadata):
# plain autoincrement/PK table in the actual schema
Table("x", metadata, Column("set_id", Integer, primary_key=True))
# for the INSERT use a table with a Sequence
# and autoincrement=False. Using a ForeignKey
# would have the same effect
some_seq = Sequence("some_seq")
dataset_no_autoinc = Table(
"x",
MetaData(),
Column(
"set_id",
Integer,
some_seq,
primary_key=True,
autoincrement=False,
),
)
return dataset_no_autoinc
示例11: test_col_w_optional_sequence_non_autoinc_no_firing
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def test_col_w_optional_sequence_non_autoinc_no_firing(
self, dataset_no_autoinc, connection
):
"""this is testing that a Table which includes a Sequence, when
run against a DB that does not support sequences, the Sequence
does not get in the way.
"""
dataset_no_autoinc.c.set_id.default.optional = True
connection.execute(dataset_no_autoinc.insert())
eq_(
connection.scalar(
select([func.count("*")]).select_from(dataset_no_autoinc)
),
1,
)
示例12: test_lastrow_accessor_four
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def test_lastrow_accessor_four(self):
metadata = MetaData()
self._test_lastrow_accessor(
Table(
"t4",
metadata,
Column(
"id",
Integer,
Sequence("t4_id_seq", optional=True),
primary_key=True,
),
Column("foo", String(30), primary_key=True),
Column("bar", String(30), server_default="hi"),
),
{"foo": "hi", "id": 1},
{"id": 1, "foo": "hi", "bar": "hi"},
)
示例13: test_freestanding_sequence_via_autoinc
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def test_freestanding_sequence_via_autoinc(self, connection):
t = Table(
"some_table",
self.metadata,
Column(
"id",
Integer,
autoincrement=True,
primary_key=True,
default=Sequence(
"my_sequence", metadata=self.metadata
).next_value(),
),
)
self.metadata.create_all(connection)
result = connection.execute(t.insert())
eq_(result.inserted_primary_key, (1,))
示例14: test_schema_translate_map_sequence
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def test_schema_translate_map_sequence(self):
s1 = schema.Sequence("s1")
s2 = schema.Sequence("s2", schema="foo")
s3 = schema.Sequence("s3", schema="bar")
schema_translate_map = {None: "z", "bar": None, "foo": "bat"}
self.assert_compile(
schema.CreateSequence(s1),
"CREATE SEQUENCE [SCHEMA__none].s1 START WITH 1",
schema_translate_map=schema_translate_map,
)
self.assert_compile(
schema.CreateSequence(s2),
"CREATE SEQUENCE [SCHEMA_foo].s2 START WITH 1",
schema_translate_map=schema_translate_map,
)
self.assert_compile(
schema.CreateSequence(s3),
"CREATE SEQUENCE [SCHEMA_bar].s3 START WITH 1",
schema_translate_map=schema_translate_map,
)
示例15: test_pickle_metadata_sequence_restated
# 需要导入模块: import sqlalchemy [as 别名]
# 或者: from sqlalchemy import Sequence [as 别名]
def test_pickle_metadata_sequence_restated(self):
m1 = MetaData()
Table(
"a",
m1,
Column("id", Integer, primary_key=True),
Column("x", Integer, Sequence("x_seq")),
)
m2 = pickle.loads(pickle.dumps(m1))
s2 = Sequence("x_seq")
t2 = Table(
"a",
m2,
Column("id", Integer, primary_key=True),
Column("x", Integer, s2),
extend_existing=True,
)
assert m2._sequences["x_seq"] is t2.c.x.default
assert m2._sequences["x_seq"] is s2