本文整理汇总了Python中sqlalchemy.sql.schema.Table方法的典型用法代码示例。如果您正苦于以下问题:Python schema.Table方法的具体用法?Python schema.Table怎么用?Python schema.Table使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.sql.schema
的用法示例。
在下文中一共展示了schema.Table方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_reflect_table_include_columns
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def test_reflect_table_include_columns(self, engine, conn):
one_row_complex = Table("one_row_complex", MetaData(bind=engine))
version = float(
re.search(r"^([\d]+\.[\d]+)\..+", sqlalchemy.__version__).group(1)
)
if version <= 1.2:
engine.dialect.reflecttable(
conn, one_row_complex, include_columns=["col_int"], exclude_columns=[]
)
else:
# https://docs.sqlalchemy.org/en/13/changelog/changelog_13.html#
# change-64ac776996da1a5c3e3460b4c0f0b257
engine.dialect.reflecttable(
conn,
one_row_complex,
include_columns=["col_int"],
exclude_columns=[],
resolve_fks=True,
)
self.assertEqual(len(one_row_complex.c), 1)
self.assertIsNotNone(one_row_complex.c.col_int)
self.assertRaises(AttributeError, lambda: one_row_complex.c.col_tinyint)
示例2: test_reflect_table_include_columns
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def test_reflect_table_include_columns(self, engine, conn):
one_row_complex = Table("one_row_complex", MetaData(bind=engine))
version = float(
re.search(r"^([\d]+\.[\d]+)\..+", sqlalchemy.__version__).group(1)
)
if version <= 1.2:
engine.dialect.reflecttable(
conn, one_row_complex, include_columns=["col_int"], exclude_columns=[],
)
else:
engine.dialect.reflecttable(
conn,
one_row_complex,
include_columns=["col_int"],
exclude_columns=[],
resolve_fks=True,
)
self.assertEqual(len(one_row_complex.c), 1)
self.assertIsNotNone(one_row_complex.c.col_int)
self.assertRaises(AttributeError, lambda: one_row_complex.c.col_tinyint)
示例3: test_to_sql
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def test_to_sql(self, engine, conn):
table_name = "to_sql_{0}".format(str(uuid.uuid4()).replace("-", ""))
df = pd.DataFrame({"a": [1, 2, 3, 4, 5]})
df.to_sql(
table_name,
engine,
schema=SCHEMA,
index=False,
if_exists="replace",
# Supported by Pandas version 0.24.0 or later.
# method="multi",
)
table = Table(table_name, MetaData(bind=engine), autoload=True)
rows = table.select().execute().fetchall()
self.assertEqual(sorted(rows), [(1,), (2,), (3,), (4,), (5,)])
示例4: transform_table
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def transform_table(self, T):
################################
# Run Table Transformations
################################
""" This will update the table 'T' in-place
(i.e. change the table's name)
"""
if not self.schema_transformer.transform_table(T):
self.logger.info(
" ---> Table ({0}) is scheduled to be deleted " +
"according to table transformations...".format(T.name))
# Clean up FKs and Indexes on this table...
del self.indexes[T.name]
del self.fks[T.name]
self.deleted_table_count += 1
self.deleted_columns += map(lambda c: T.name +
"." + c.name, T.columns)
self.deleted_column_count += len(T.columns)
return None
return True
示例5: check_multiple_autoincrement_issue
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def check_multiple_autoincrement_issue(self, auto_inc_count, pk_count, T):
if pk_count > 1:
# Sometimes we can't detect the 'autoincrement' attr on columns
# (For instance on SQL Server...)
for c in T.columns:
if c.primary_key:
c.autoincrement = False
# and engine == MySQL.innoDB...
if auto_inc_count > 0:
# print the verbose warning
self.logger.warning("""
****************************************************************
**** Table '{0}' contains a composite primary key,
**** with an auto-increment attribute tagged on 1 of the columns.
*****************************************************************
********* --We are dropping the auto-increment field-- **********
*****************************************************************
** (why? MySQL -> InnoDB Engine does not support this.
** Try MyISAM for support - understand that Oracle does not allow
** auto-increment fields, but uses sequences to create unique
** composite PKs")
*****************************************************************
""".format(T.name))
示例6: create_table
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def create_table(self, T_dst_exists, T):
with self.dst_engine.connect() as conn:
if not T_dst_exists:
self.logger.info(" --> Creating table '{0}'".format(T.name))
try:
T.create(conn)
return True
except Exception as e:
self.logger.error(
"Failed to create table '{0}'\n\n{1}".format(
T.name, e))
raise
else:
self.logger.warning(
"Table '{0}' already exists - not creating table, " +
"reflecting to get new changes instead..".format(T.name))
self.tgt_insp.reflecttable(T, None)
return True
# We need to Upsert the data...
示例7: setUp
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def setUp(self):
from gengine.app.cache import clear_all_caches
clear_all_caches()
self.db = db.db()
dsn = self.db.dsn()
self.engine = create_engine(
"postgresql://%(user)s@%(host)s:%(port)s/%(database)s" % {
"user": dsn["user"],
"host": dsn["host"],
"port": dsn["port"],
"database": dsn["database"],
}
)
init_session(override_session=scoped_session(get_sessionmaker(bind=self.engine)), replace=True)
from gengine.metadata import Base
Base.metadata.bind = self.engine
Base.metadata.drop_all(self.engine)
self.engine.execute("DROP SCHEMA IF EXISTS public CASCADE")
self.engine.execute("CREATE SCHEMA IF NOT EXISTS public")
from alembic.config import Config
from alembic import command
alembic_cfg = Config(attributes={
'engine': self.engine,
'schema': 'public'
})
script_location = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
'app/alembic'
)
alembic_cfg.set_main_option("script_location", script_location)
from gengine.app import model
tables = [t for name, t in model.__dict__.items() if isinstance(t, Table)]
Base.metadata.create_all(self.engine, tables=tables)
command.stamp(alembic_cfg, "head")
示例8: test_reflect_no_such_table
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def test_reflect_no_such_table(self, engine, conn):
self.assertRaises(
NoSuchTableError,
lambda: Table("this_does_not_exist", MetaData(bind=engine), autoload=True),
)
self.assertRaises(
NoSuchTableError,
lambda: Table(
"this_does_not_exist",
MetaData(bind=engine),
schema="also_does_not_exist",
autoload=True,
),
)
示例9: test_reflect_table
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def test_reflect_table(self, engine, conn):
one_row = Table("one_row", MetaData(bind=engine), autoload=True)
self.assertEqual(len(one_row.c), 1)
self.assertIsNotNone(one_row.c.number_of_rows)
示例10: test_reflect_table_with_schema
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def test_reflect_table_with_schema(self, engine, conn):
one_row = Table("one_row", MetaData(bind=engine), schema=SCHEMA, autoload=True)
self.assertEqual(len(one_row.c), 1)
self.assertIsNotNone(one_row.c.number_of_rows)
示例11: test_unicode
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def test_unicode(self, engine, conn):
unicode_str = "密林"
one_row = Table("one_row", MetaData(bind=engine))
returned_str = sqlalchemy.select(
[expression.bindparam("あまぞん", unicode_str)], from_obj=one_row,
).scalar()
self.assertEqual(returned_str, unicode_str)
示例12: test_char_length
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def test_char_length(self, engine, conn):
one_row_complex = Table("one_row_complex", MetaData(bind=engine), autoload=True)
result = (
sqlalchemy.select(
[sqlalchemy.func.char_length(one_row_complex.c.col_string)]
)
.execute()
.scalar()
)
self.assertEqual(result, len("a string"))
示例13: test_reflect_select
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def test_reflect_select(self, engine, conn):
one_row_complex = Table("one_row_complex", MetaData(bind=engine), autoload=True)
self.assertEqual(len(one_row_complex.c), 15)
self.assertIsInstance(one_row_complex.c.col_string, Column)
rows = one_row_complex.select().execute().fetchall()
self.assertEqual(len(rows), 1)
self.assertEqual(
list(rows[0]),
[
True,
127,
32767,
2147483647,
9223372036854775807,
0.5,
0.25,
"a string",
datetime(2017, 1, 1, 0, 0, 0),
date(2017, 1, 2),
b"123",
"[1, 2]",
"{1=2, 3=4}",
"{a=1, b=2}",
Decimal("0.1"),
],
)
self.assertIsInstance(one_row_complex.c.col_boolean.type, BOOLEAN)
self.assertIsInstance(one_row_complex.c.col_tinyint.type, INTEGER)
self.assertIsInstance(one_row_complex.c.col_smallint.type, INTEGER)
self.assertIsInstance(one_row_complex.c.col_int.type, INTEGER)
self.assertIsInstance(one_row_complex.c.col_bigint.type, BIGINT)
self.assertIsInstance(one_row_complex.c.col_float.type, FLOAT)
self.assertIsInstance(one_row_complex.c.col_double.type, FLOAT)
self.assertIsInstance(one_row_complex.c.col_string.type, type(STRINGTYPE))
self.assertIsInstance(one_row_complex.c.col_timestamp.type, TIMESTAMP)
self.assertIsInstance(one_row_complex.c.col_date.type, DATE)
self.assertIsInstance(one_row_complex.c.col_binary.type, BINARY)
self.assertIsInstance(one_row_complex.c.col_array.type, type(STRINGTYPE))
self.assertIsInstance(one_row_complex.c.col_map.type, type(STRINGTYPE))
self.assertIsInstance(one_row_complex.c.col_struct.type, type(STRINGTYPE))
self.assertIsInstance(one_row_complex.c.col_decimal.type, DECIMAL)
示例14: test_reserved_words
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def test_reserved_words(self, engine, conn):
"""Presto uses double quotes, not backticks"""
fake_table = Table(
"select", MetaData(bind=engine), Column("current_timestamp", STRINGTYPE)
)
query = str(fake_table.select(fake_table.c.current_timestamp == "a"))
self.assertIn('"select"', query)
self.assertIn('"current_timestamp"', query)
self.assertNotIn("`select`", query)
self.assertNotIn("`current_timestamp`", query)
示例15: _set_table
# 需要导入模块: from sqlalchemy.sql import schema [as 别名]
# 或者: from sqlalchemy.sql.schema import Table [as 别名]
def _set_table(self, table: Column, column: Table) -> None:
self.impl._set_table(table, column)