sky*_*ler 9 python orm sqlalchemy alembic sql-domain
我开始将Alembic合并到我已经使用SQLAlchemy表定义的项目中.目前我的数据库架构是在我的应用程序外部管理的,我想将整个架构带入我的表定义文件中.
在PostgreSQL中,我使用自定义域来存储电子邮件地址.PostgreSQL DDL是:
CREATE DOMAIN email_address TEXT CHECK (value ~ '.+@.+')
Run Code Online (Sandbox Code Playgroud)
如何在SQLAlchemy中表示此域的创建及其作为列数据类型的用法?
这可能远不是一个有效的解决方案,但我认为最好的方法是 subclass sqlalchemy.schema._CreateDropBase。
from sqlalchemy.schema import _CreateDropBase
class CreateDomain(_CreateDropBase):
'''Represent a CREATE DOMAIN statement.'''
__visit_name__ = 'create_domain'
def __init__(self, element, bind=None, **kw):
super(CreateDomain, self).__init__(element, bind=bind, **kw)
class DropDomain(_CreateDropBase):
'''Represent a DROP BASE statement.'''
__visit_name__ = 'drop_domain'
def __init__(self, element, bind=None, **kw):
super(DropDomain, self).__init__(element, bind=bind, **kw)
@compiles(CreateDomain, 'postgresql')
def visit_create_domain(element, compiler, **kw):
text = '\nCREATE DOMAIN %s AS %s' % (
compiler.prepare.format_column(element.name),
compiler.preparer.format_column(element.type_)) # doesn't account for arrays and such I don't think
default = compiler.get_column_default_string(column)
if default is not None:
text += " DEFAULT %s" % default
return text
Run Code Online (Sandbox Code Playgroud)
显然,这并不完整,但如果您非常想要这个,它应该为您提供一个良好的起点。:)
| 归档时间: |
|
| 查看次数: |
545 次 |
| 最近记录: |