I’ve been trying to follow this tutorial but I changed my code slightly to understand better, but I’m getting a error that there is a missing column when I’m trying to create a new resource via a post request, I’m utilizing flask
, flask_restful
and flask_alchemy
Model definition:
class UserModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), nullable=False)
def __repr__(self):
return f"User(name={name}, email={email})"
User class definition:
# user class
class User(Resource):
@marshal_with(resource_fields_user)
def get(self, user_id):
result = UserModel.query.get(id=user_id)
return {"request_type": "get", "data": result}
# @marshal_with(resource_fields_user)
def post(self):
args = user_post_args.parse_args()
user = UserModel(name=args["name"], email=args["email"])
db.session.add(user)
db.session.commit()
return {"request_type": "post", "data": user}, 201
The post request:
curl --location 'http://127.0.0.1:5000/users'
--header 'Content-Type: application/json'
--data-raw '{
"name": "Jane Doe",
"email": "[email protected]"
}'
But when I run a post to this I get the following error:
sqlalchemy.exc.OperationalError
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) table user_model has no column named email [SQL: INSERT INTO user_model (name, email) VALUES (?, ?)] [parameters: ('Jane Doe', '[email protected]')] (Background on this error at: http://sqlalche.me/e/13/e3q8)
Trying to figure out why it thinks that I don’t have an email column?
1