使用PostgreSQL的SQLAlchemy中的ENUM类型

编程入门 行业动态 更新时间:2024-10-25 00:27:21
本文介绍了使用PostgreSQL的SQLAlchemy中的ENUM类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用一个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类型

本文发布于:2023-10-16 16:50:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   PostgreSQL   SQLAlchemy   ENUM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!