任何人都可以告诉我为什么下面的DateTime类型创建一个“DATE”对象而不是DateTime(或更恰当地,TIMESTAMP类型),因为我必须强制类型如下面的行:
#!/bin/python
import sqlalchemy
from sqlalchemy import Column, Integer, String, DateTime, Index, MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm import sessionmaker
from sqlalchemy.dialects import oracle
Base = declarative_base()
import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
class TypeTest(Base):
__tablename__ = "TYPETESTZ"
thisisinteger = Column(Integer, primary_key = True)
thisisnotadatetime = Column(DateTime)
thisisdatetime = Column(oracle.TIMESTAMP)
if __name__ == "__main__":
engine = sqlalchemy.create_engine('oracle://richard:password@xe')
metadata = Base.metadata
metadata.create_all(engine)
日志输出:
INFO:sqlalchemy.engine.base.Engine:SELECT USER FROM DUAL
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:SELECT table_name FROM all_tables WHERE table_name = :name AND owner = :schema_name
INFO:sqlalchemy.engine.base.Engine:{'name': u'TYPETESTZ', 'schema_name': u'RICHARD'}
INFO:sqlalchemy.engine.base.Engine:
CREATE TABLE "TYPETESTZ" (
thisisinteger INTEGER NOT NULL,
thisisnotadatetime DATE,
thisisdatetime TIMESTAMP,
PRIMARY KEY (thisisinteger)
)
INFO:sqlalchemy.engine.base.Engine:{}
INFO:sqlalchemy.engine.base.Engine:COMMIT
最佳答案 Oracle没有DateTime数据类型.在Oracle中,日期包含一天(即2012年8月21日)和时间(即下午1:30),粒度为1秒.没有其他限定符的时间戳只允许您将粒度增加到纳秒(10 ^ -9秒). SQLAlchemy将DateTime转换为Oracle Date似乎是合理的,除非有一些要求DateTime支持小数秒(在这种情况下你需要一个时间戳)或时区(在这种情况下你需要一个带有[local]的时间戳)时区).