看起来SQLAlchemy默认将VARCHAR2
列创建为CHAR
。如何使用BYTE
创建它?
from sqlalchemy import MetaData, Column, String
from sqlalchemy.ext.declarative import declarative_base
metadata = MetaData()
Base = declarative_base(metadata=metadata)
class Foo(Base):
__tablename__ = 'foo'
name = Column(String(10), primary_key=True)
Foo.__table__.create(bind=engine)
这将创建下表:
CREATE TABLE XXMD.FOO
(
NAME VARCHAR2(10 CHAR) NOT NULL
)
相反,我希望它创建以下内容:
CREATE TABLE XXMD.FOO
(
NAME VARCHAR2(10 BYTE) NOT NULL
)
答案 0 :(得分:1)
感谢Mike Bayer:
from sqlalchemy.ext.compiler import compiles
class VARCHAR2Byte(String):
pass
@compiles(VARCHAR2Byte)
def compile_varchar2_byte(type_, compiler, **kw):
len = type_.length
return 'VARCHAR2(%i BYTE)' % len