本文整理汇总了Python中sqlalchemy.types.VARCHAR属性的典型用法代码示例。如果您正苦于以下问题:Python types.VARCHAR属性的具体用法?Python types.VARCHAR怎么用?Python types.VARCHAR使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类sqlalchemy.types
的用法示例。
在下文中一共展示了types.VARCHAR属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_enum_detection
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [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'))
)
"""
)
示例2: test_mysql_column_types
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_mysql_column_types(metadata):
Table("simple_items", metadata, Column("id", mysql.INTEGER), Column("name", mysql.VARCHAR(255)))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer, MetaData, String, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('id', Integer),
Column('name', String(255))
)
"""
)
示例3: test_indexes_table
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_indexes_table(metadata):
simple_items = Table("simple_items", metadata, Column("id", INTEGER), Column("number", INTEGER), Column("text", VARCHAR))
simple_items.indexes.add(Index("idx_number", simple_items.c.number))
simple_items.indexes.add(Index("idx_text_number", simple_items.c.text, simple_items.c.number, unique=True))
simple_items.indexes.add(Index("idx_text", simple_items.c.text, unique=True))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Index, Integer, MetaData, String, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('id', Integer),
Column('number', Integer, index=True),
Column('text', String, unique=True),
Index('idx_text_number', 'text', 'number', unique=True)
)
"""
)
示例4: test_foreign_key_options
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [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'))
)
"""
)
示例5: test_metadata_column
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_metadata_column(metadata):
Table("simple", metadata, Column("id", INTEGER, primary_key=True), Column("metadata", VARCHAR))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class Simple(Base):
__tablename__ = 'simple'
id = Column(Integer, primary_key=True)
metadata_ = Column('metadata', String)
"""
)
示例6: str_to_sqltype
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def str_to_sqltype(expr):
import re
import sqlalchemy.types as sqltypes
norm_expr = expr.lower()
if norm_expr.startswith('integer'):
match_result = re.match(r'integer\((\d+)\)', norm_expr)
if match_result is not None:
return sqltypes.BIGINT() if int(match_result.group(1)) > 11 else sqltypes.INTEGER()
return sqltypes.BIGINT()
if norm_expr == 'decimal':
return sqltypes.DECIMAL()
if norm_expr == 'date':
return sqltypes.DATETIME()
if norm_expr == 'bool' or norm_expr == 'boolean':
return sqltypes.BOOLEAN()
if norm_expr.startswith('string'):
match_result = re.match(r'string\((\d+)\)', norm_expr)
if match_result is not None:
maxlen = int(match_result.group(1))
return sqltypes.VARCHAR(maxlen) if maxlen < 65536 else sqltypes.TEXT
return sqltypes.TEXT()
raise RuntimeError("Unsupported data type [" + expr + "]")
示例7: test_varchar_as_nulltype
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_varchar_as_nulltype(self):
"""
Varchar columns with no length should be considered NullType columns
"""
dialect = RedshiftDialect()
column_info = dialect._get_column_info(
'Null Column',
'character varying', None, False, {}, {}, 'default', 'test column'
)
assert isinstance(column_info['type'], NullType)
column_info_1 = dialect._get_column_info(
'character column',
'character varying(30)', None, False, {}, {}, 'default',
comment='test column'
)
assert isinstance(column_info_1['type'], VARCHAR)
示例8: test_default_reflection_2
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_default_reflection_2(self):
db = testing.db
m = MetaData(db)
expected = ["'my_default'", "0"]
table = """CREATE TABLE r_defaults (
data VARCHAR(40) DEFAULT 'my_default',
val INTEGER NOT NULL DEFAULT 0
)"""
try:
exec_sql(db, table)
rt = Table("r_defaults", m, autoload=True)
for i, reflected in enumerate(rt.c):
eq_(str(reflected.server_default.arg), expected[i])
finally:
exec_sql(db, "DROP TABLE r_defaults")
示例9: test_default_reflection_3
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_default_reflection_3(self):
db = testing.db
table = """CREATE TABLE r_defaults (
data VARCHAR(40) DEFAULT 'my_default',
val INTEGER NOT NULL DEFAULT 0
)"""
try:
exec_sql(db, table)
m1 = MetaData(db)
t1 = Table("r_defaults", m1, autoload=True)
exec_sql(db, "DROP TABLE r_defaults")
t1.create()
m2 = MetaData(db)
t2 = Table("r_defaults", m2, autoload=True)
self.assert_compile(
CreateTable(t2),
"CREATE TABLE r_defaults (data VARCHAR(40) "
"DEFAULT 'my_default', val INTEGER DEFAULT 0 "
"NOT NULL)",
)
finally:
exec_sql(db, "DROP TABLE r_defaults")
示例10: test_on_conflict_clause_unique_constraint
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_on_conflict_clause_unique_constraint(self):
meta = MetaData()
t = Table(
"n",
meta,
Column("id", Integer),
Column("x", String(30)),
UniqueConstraint("id", "x", sqlite_on_conflict="FAIL"),
)
self.assert_compile(
CreateTable(t),
"CREATE TABLE n (id INTEGER, x VARCHAR(30), "
"UNIQUE (id, x) ON CONFLICT FAIL)",
dialect=sqlite.dialect(),
)
示例11: test_on_conflict_clause_primary_key_constraint_from_column
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_on_conflict_clause_primary_key_constraint_from_column(self):
meta = MetaData()
t = Table(
"n",
meta,
Column(
"x",
String(30),
sqlite_on_conflict_primary_key="FAIL",
primary_key=True,
),
)
self.assert_compile(
CreateTable(t),
"CREATE TABLE n (x VARCHAR(30) NOT NULL, "
"PRIMARY KEY (x) ON CONFLICT FAIL)",
dialect=sqlite.dialect(),
)
示例12: test_on_conflict_clause_primary_key_constraint
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_on_conflict_clause_primary_key_constraint(self):
meta = MetaData()
t = Table(
"n",
meta,
Column("id", Integer),
Column("x", String(30)),
PrimaryKeyConstraint("id", "x", sqlite_on_conflict="FAIL"),
)
self.assert_compile(
CreateTable(t),
"CREATE TABLE n ("
"id INTEGER NOT NULL, "
"x VARCHAR(30) NOT NULL, "
"PRIMARY KEY (id, x) ON CONFLICT FAIL)",
dialect=sqlite.dialect(),
)
示例13: test_no_clobs_for_string_params
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_no_clobs_for_string_params(self):
"""test that simple string params get a DBAPI type of
VARCHAR, not CLOB. This is to prevent setinputsizes
from setting up cx_oracle.CLOBs on
string-based bind params [ticket:793]."""
class FakeDBAPI(object):
def __getattr__(self, attr):
return attr
dialect = oracle.OracleDialect()
dbapi = FakeDBAPI()
b = bindparam("foo", "hello world!")
eq_(b.type.dialect_impl(dialect).get_dbapi_type(dbapi), "STRING")
b = bindparam("foo", "hello world!")
eq_(b.type.dialect_impl(dialect).get_dbapi_type(dbapi), "STRING")
示例14: test_reflect_unicode_no_nvarchar
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_reflect_unicode_no_nvarchar(self):
metadata = self.metadata
Table("tnv", metadata, Column("data", sqltypes.Unicode(255)))
metadata.create_all()
m2 = MetaData(testing.db)
t2 = Table("tnv", m2, autoload=True)
assert isinstance(t2.c.data.type, sqltypes.VARCHAR)
if testing.against("oracle+cx_oracle"):
assert isinstance(
t2.c.data.type.dialect_impl(testing.db.dialect),
cx_oracle._OracleString,
)
data = u("m’a réveillé.")
t2.insert().execute(data=data)
res = t2.select().execute().first()["data"]
eq_(res, data)
assert isinstance(res, util.text_type)
示例15: test_char_length
# 需要导入模块: from sqlalchemy import types [as 别名]
# 或者: from sqlalchemy.types import VARCHAR [as 别名]
def test_char_length(self):
metadata = self.metadata
t1 = Table(
"t1",
metadata,
Column("c1", VARCHAR(50)),
Column("c2", NVARCHAR(250)),
Column("c3", CHAR(200)),
Column("c4", NCHAR(180)),
)
t1.create()
m2 = MetaData(testing.db)
t2 = Table("t1", m2, autoload=True)
eq_(t2.c.c1.type.length, 50)
eq_(t2.c.c2.type.length, 250)
eq_(t2.c.c3.type.length, 200)
eq_(t2.c.c4.type.length, 180)