我正在使用一个postgresql数据库的 SQLAlchemy core ,我想将 ENUM 类型添加到我的表定义。根据 postgresql文档,必须先定义ENUM类型创建表:
I'm using SQLAlchemy core with a postgresql database and I would like to add the ENUM type to my table definition. According to the postgresql documentation, the ENUM type must be defined prior the table is created:
CREATE TYPE gender_enum AS ENUM ('female', 'male'); CREATE TABLE person ( name VARCHAR(20), gender gender_enum );问题是当我创建表定义时。阅读 SQLAlchemy文档后,我可以找不到任何实现的例子。我已经尝试过这样的东西,但它没有工作:
The problem is when I'm creating the table definition. After reading the SQLAlchemy documentation I couldn't find any implementation example. I've tried something like this but it didn't work:
from sqlalchemy.dialects.postgresql import ENUM person = Table('user_profile', metadata, Column('name', String(20)), Column('gender', ENUM('female','male')) );如何完成?
推荐答案您需要从sqlalchemy导入Enum并为其添加名称。它应该像这样工作:
You need to import Enum from a sqlalchemy and add a name to it. It should work like this:
from sqlalchemy import Enum person = Table("user_profile", metadata, Column("name", String(20)), Column("gender", Enum("female", "male", name="gender_enum", create_type=False)) );更多推荐
使用PostgreSQL的SQLAlchemy中的ENUM类型
发布评论