ash*_*h84 6 mysql sqlalchemy flask-sqlalchemy
我想添加一个tabble和列创建的注释.
所以我将doc参数添加到SQLAlchemy Column类的构造函数中.
但是不要在列中添加注释.
class Notice(db.Model):
__tablename__ = "tb_notice"
__table_args__ = {'mysql_engine': 'MyISAM'}
seqno = db.Column(db.Integer, primary_key=True, autoincrement=True, doc="seqno")
title = db.Column(db.String(200), nullable=False, doc="notice title")
detail = db.Column(db.TEXT, nullable=True, doc="notice detail ")
Run Code Online (Sandbox Code Playgroud)
我想知道如何添加表的注释.
Res*_*awi 10
根据参数文档doc
:
\n\ndoc\xc2\xb6 \xe2\x80\x93 可选 可由 ORM 使用的字符串或类似于 Python 端的文档属性。该属性不渲染SQL注释;为此目的,请使用 Column.comment 参数。
\n
以及comment
参数:
\n\ncomment\xc2\xb6 \xe2\x80\x93 可选字符串,将在表创建时呈现 SQL 注释。
\n
请注意,comment
在 1.2 版本中添加了SQlAlchemy
为了添加表的注释,您只需将附加comment
属性(根据Table
类文档)传递给您的__table_args__
字典。1.2版本中也添加了
代码会是这样的:
\nclass Notice(db.Model):\n \n __tablename__ = "tb_notice"\n __table_args__ = {\n \'mysql_engine\': \'MyISAM\',\n \'comment\': \'Notice table\'\n }\n\n seqno = db.Column(db.Integer, primary_key=True, autoincrement=True, doc="seqno",\n comment=\'Integer representing the sequence number\')\n title = db.Column(db.String(200), nullable=False, doc="notice title",\n comment=\'Title of the notice, represented as a string\')\n detail = db.Column(db.TEXT, nullable=True, doc="notice detail",\n comment=\'Notice detail description\')\n
Run Code Online (Sandbox Code Playgroud)\n该doc
属性充当类的文档字符串:
print(Notice.title.__doc__)\n
Run Code Online (Sandbox Code Playgroud)\n将输出:\nnotice title
现在相应的SQL
建表语句是:
CREATE TABLE `tb_notice` (\n `seqno` int(11) NOT NULL COMMENT \'Integer representing the sequence number\',\n `title` varchar(200) NOT NULL COMMENT \'Title of the notice, represented as a string\',\n `detail` text COMMENT \'Notice detail description\'\n) ENGINE=MyISAM DEFAULT CHARSET=utf32 COMMENT=\'Notice table\';\n
Run Code Online (Sandbox Code Playgroud)\n您可以看到注释已正确添加到表和列中。
\n版本1.2中仅支持注释版本1.2中的新功能:http: //docs.sqlalchemy.org/en/latest/core/metadata.html#sqlalchemy.schema.Column.params.comment
归档时间: |
|
查看次数: |
4532 次 |
最近记录: |