11

Hello I am trying to create table using flask-SQLAlchmey here is the code

class User(UserMixin, db.Model):

__tablename__ = 'mailbox'

id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), index=True, nullable=False)
password = db.Column(db.String(255), nullable=False)
name = db.Column(db.String(100), nullable=False)
maildir = db.Column(db.String(100), nullable=False)
alias = db.Column(db.Enum('N','Y'), nullable=False, default="N")

After running db init, migrate here is the output of mysql show create table

| mailbox | CREATE TABLE `mailbox` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `maildir` varchar(100) NOT NULL,
  `alias` enum('N','Y') NOT NULL,
  PRIMARY KEY (`id`),
  KEY `ix_mailbox_username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

The output shows that default value of enum is not there. Would you please help me, what I need to do to fix this.

6
  • Check this stackoverflow.com/questions/9706059/… Commented Jun 3, 2017 at 11:34
  • in short don't bother with enum! Commented Jun 3, 2017 at 11:38
  • 1
    No idea what is the helpful form me in the above link Commented Jun 3, 2017 at 11:42
  • My Requirement is enum both php and python using this table Commented Jun 3, 2017 at 11:43
  • 4
    Here is the correct answer alias = db.Column(db.Enum('N','Y'), nullable=False, server_default=("N")) Commented Jun 3, 2017 at 12:40

3 Answers 3

6

In addition to Umar Draz answer, here you can find the documentation:

https://docs.sqlalchemy.org/en/13/core/metadata.html#sqlalchemy.schema.Column.params.server_default

for enum you have to use parenthesis:

server_default=("")
Sign up to request clarification or add additional context in comments.

1 Comment

("") is exactly equivalent to "". Did you mean ("",)?
5

I had the same issue, I resolved it changine 'default' by 'server_default', as following:

alias = db.Column(db.Enum('N','Y'), nullable=False, server_default="N")

Comments

0

Problem

I had a similar issue with Enum and default value. I had the code below which gave me the error:

sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input value for enum taskcategory: "todo"
LINE 8:  category taskcategory DEFAULT 'todo' NOT NULL

The code that gave me this error looked roughly like this:

from enum import Enum

from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from src.sql_models.BaseModel import Base
import enum

import sqlalchemy


class Base(DeclarativeBase):
    __abstract__ = True
    type_annotation_map = {
        enum.Enum: sqlalchemy.Enum(enum.Enum),
    }


class TaskCategory(Enum):
    TODO = "todo"
    DOING = "doing"
    FINISHED = "finished"


class Task(Base):
    __tablename__ = "tasks"

    title: Mapped[str] = mapped_column(nullable=False)
    category: Mapped[TaskCategory] = mapped_column(
        server_default=TaskCategory.TODO.value
    )

Observation

When sqlalchemy was trying to build the tables using

Base.metadata.create_all(database_engine)

I noticed the following amongst all the SQL logs:

sqlalchemy.engine.Engine CREATE TYPE taskcategory AS ENUM ('TODO', 'DOING', 'FINISHED')

So it seems like the enum values that get created in SQLAlchemy are uppercase.

My solution

Simply changing my Enum to using uppercase values solved the issue:

class TaskCategory(Enum):
    TODO = "TODO"
    DOING = "DOING"
    FINISHED = "FINISHED"

Hope this helps anyone.

4 Comments

The enum is created using the names of the enum, not the values. So given TODO = 'todo', the enum in the database is TODO
But why did it help using uppercase for the values instead of lowercase?
Because then the value (which you are using as server_default ) matches the definition in the database (both are TODO).
Oh okay, I think I get it. Thanks for the explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.