Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask SQLAlchemy enum field default value

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.

like image 224
Umar Draz Avatar asked Jun 03 '17 11:06

Umar Draz


2 Answers

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=("")
like image 133
julste Avatar answered Oct 21 '22 13:10

julste


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")
like image 20
juanjosegdoj Avatar answered Oct 21 '22 14:10

juanjosegdoj