SQLAlchemy和Oracle - 如何让VARCHAR2列使用BYTE而不是CHAR?

时间:2017-08-01 22:54:11

标签: python oracle sqlalchemy

看起来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
)

1 个答案:

答案 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