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.
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=("")
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With