Issue
This is the model
from sqlalchemy.dialects import postgresql
class MyModel(db.Model):
__tablename__ = 'mytables'
id = db.Column(db.Integer, primary_key=True)
datetime = db.Column(db.DateTime, nullable=False)
option = db.Column(postgresql.ENUM('YES', 'NO', 'MAYBE', name='_option', create_type=False), nullable=False)
data = db.Column(db.String(20))
This is the class I use to store the data
class myList(Resource):
parser = reqparse.RequestParser()
def post(self):
data = myList.parser.parse_args()
my_json = request.get_json()
my_data = my_schema.load(my_json)
my_data.save()
return my_schema.dump(my_data), 201
This is my schema
from ma import ma
from models.mymodel import MyModel
class MySchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = MyModel
load_instance = True
include_fk = True
And this is the complete error message, assuming I give YES input
sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input value for enum status_option: ""YES""
Solution
Finally I found the solution
so below the model table I put on the question, I have this method
def __init__(self, datetime, option, data):
self.datetime = datetime
self.option = json.dumps(option)
self.data = data
then I changed it into this and its working
def __init__(self, datetime, option, data):
self.datetime = datetime
self.option = option
self.data = data
Answered By - Aditya
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.