根据
doc
参数的
文档说明:
doc¶ - 可选的字符串,可用于ORM或类似工具文档化Python端上的属性。 该属性不会呈现SQL注释;用Column.comment参数代替。
而
comment
参数如下:
comment¶ - 可选的字符串,在表创建时渲染为SQL注释。
请注意,自
SQlAlchemy
版本1.2起添加了
comment
。
如果要为表添加注释,只需将附加的
comment
属性(根据
Table
类的
文档)传递给您的
__table_args__
字典。也是从版本1.2开始添加的。
代码应该像这样:
class Notice(db.Model):
__tablename__ = "tb_notice"
__table_args__ = {
'mysql_engine': 'MyISAM',
'comment': 'Notice table'
}
seqno = db.Column(db.Integer, primary_key=True, autoincrement=True, doc="seqno",
comment='Integer representing the sequence number')
title = db.Column(db.String(200), nullable=False, doc="notice title",
comment='Title of the notice, represented as a string')
detail = db.Column(db.TEXT, nullable=True, doc="notice detail",
comment='Notice detail description')
doc
属性充当您的类的文档字符串:
print(Notice.title.__doc__)
将输出:
通知标题
现在对应的 SQL
表创建语句如下:
CREATE TABLE `tb_notice` (
`seqno` int(11) NOT NULL COMMENT 'Integer representing the sequence number',
`title` varchar(200) NOT NULL COMMENT 'Title of the notice, represented as a string',
`detail` text COMMENT 'Notice detail description'
) ENGINE=MyISAM DEFAULT CHARSET=utf32 COMMENT='Notice table';
你可以看到注释已经正确地添加到了表格和列中。