模型类default关键字在sqlalchemy中是如何工作的

2024-04-20 10:09:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我已将模型类中列的映射设置为

class Plan(Base):
    __tablename__ = "plans"
    `default_c = Column(Boolean, default=False)`

但是当我使用插入数据到这个表时。以下代码。我仍然得到(exceptions.TypeError) Not a boolean value: ''。我的字典中有default_c字段为空。我想知道default是否应该处理这个问题。你知道吗

conn.execute(Plan.__table__.insert(), Plan_dict) 

Plan\ U dict是我要插入plans表的词典列表。你知道吗


Tags: 数据代码模型falsedefaultbasecolumndict
1条回答
网友
1楼 · 发布于 2024-04-20 10:09:07

根据documentation,如果您不提供列,那么它将填充默认值

A scalar, Python callable, or ColumnElement expression representing the default value for this column, which will be invoked upon insert if this column is otherwise not specified in the VALUES clause of the insert. This is a shortcut to using ColumnDefault as a positional argument; see that class for full detail on the structure of the argument.

我觉得你给了default_c一个空值。从Plan_dict中删除default_c并尝试一下。你知道吗

相关问题 更多 >