我正在使用SQLalchemy在一个遗留的MSSQL数据库中工作,我有一个声明性映射。
这个数据库有几个表具有计算列。我可以很好地阅读它们,但是(当然)写入计算列是行不通的。但是,当我创建并试图保存一个ORM对象时,SQLAlchemy仍然试图在这些列中保存“None”值,从而导致错误。在
{alci>在实现
代码如下-映射:
class transactionlog(Base):
__tablename__ = 'transactionlog'
tlog_id = Column(VARCHAR(length=36), primary_key=True, nullable=False)
tlog_ppl_id = Column(VARCHAR(length=36), ForeignKey('people.ppl_id'))
tlog_evtt_id = Column(VARCHAR(length=5))
tlog_testrun = Column(BIT())
tlog_Data = Column(NVARCHAR(length=300))
tlog_price = Column(DECIMAL(precision=18, scale=2))
tlog_comment = Column(NVARCHAR(length=1000))
_tlog_real_timehh = Column('tlog_real_timehh', INTEGER())
_tlog_real_timemm = Column('tlog_real_timemm', INTEGER())
_tlog_real_timess = Column('tlog_real_timess', INTEGER())
_tlog_fin_booking = Column('tlog_fin_booking', BIT())
@hybrid_property
def tlog_real_timehh(self):
return self._tlog_real_timehh
@tlog_real_timehh.setter
def tlog_real_timehh(self, tlog_real_timehh):
self._tlog_real_timehh = tlog_real_timehh
@hybrid_property
def tlog_real_timemm(self):
return self._tlog_real_timemm
@tlog_real_timemm.setter
def tlog_real_timemm(self, tlog_real_timemm):
self._tlog_real_timemm = tlog_real_timemm
@hybrid_property
def tlog_real_timess(self):
return self._tlog_real_timess
@tlog_real_timess.setter
def tlog_real_timess(self, tlog_real_timess):
self._tlog_real_timess = tlog_real_timess
@hybrid_property
def tlog_fin_booking(self):
return self._tlog_fin_booking
@tlog_fin_booking.setter
def tlog_fin_booking(self, tlog_fin_booking):
self._tlog_fin_booking = tlog_fin_booking
以及应该添加新记录的代码:
^{pr2}$我希望hybrid_属性代码将计算字段设为只读,但是SQLAlchemy似乎仍然根据映射代码在INSERT语句中填充它们。(当我查看SQL语句时可以看到这一点。我不能发布SQL语句,因为我对对象进行了一些缩写,使其在StackOverflow上没有任何敏感数据)。在
问题是,为什么SQLAlchemy仍然尝试为tlog_real_timehh、tlog_real_timemm、tlog_real_times和tlog_fin_预订插入值,以及如何防止这种情况?在
谢谢你给我的任何建议。
埃里克
用^{} 标记server-generated columns:
相关问题 更多 >
编程相关推荐