Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I'm trying to get a simple POST/rest api off the ground and am getting the following error:
- 2017-06-12 18:14:51,466] ERROR in app: Exception on /profit [POST]
- Traceback (most recent call last):
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
- response = self.full_dispatch_request()
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
- rv = self.handle_user_exception(e)
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
- return cors_after_request(app.make_response(f(*args, **kwargs)))
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
- reraise(exc_type, exc_value, tb)
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
- rv = self.dispatch_request()
- File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
- return self.view_functions[rule.endpoint](**req.view_args)
- File "/home/patientplatypus/vuespring/backend_PYTHON3/main.py", line 52, in profits
- cur.execute(sql, params)
- IntegrityError: null value in column "created_at" violates not-null constraint
- DETAIL: Failing row contains (1, testname, testdescription, null, null).
- So when I post I call this from main:
- app.route('/profit', methods=['POST','GET'])
- def profits():
- if request.method=='POST':
- conn = psycopg2.connect(database = "money", user = "patientplatypus", password = "Fvnjty0b")
- cur = conn.cursor()
- sql = "INSERT INTO PROFITS (ID,NAME,DESCRIPTION) VALUES (%s, %s, %s)"
- params = (request.json['id'], request.json['name'], request.json['description'])
- cur.execute(sql, params)
- conn.commit()
- print "Records created successfully";
- conn.close()
- #
- # profit = {
- # 'id':request.json['id'],
- # 'name':request.json['name'],
- # 'description':request.json['description']
- # }
- # profits.append(profit)
- So here I am only assigning three variables to be written into my table (id,name,description). However in my profits function I also have a created at variable which I have not specified in the post, which is probably throwing the exception. I am looking for a way to either get rid of this created at variable (gettting rid of it doesnt get rid of the exception), or make it non-required. I set nullable=TRUE so that it could except not being assigned, but this hasnt worked as I expected. Does anyone have any suggestions?
- This is my profits.py file that is breaking
- from shared import db, dump_datetime
- import datetime
- class Profit(db.Model):
- __tablename__ = 'profits'
- id = db.Column(db.Integer, primary_key=True)
- name = db.Column(db.String(100), unique=True, nullable=True)
- description = db.Column(db.Text)
- created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=True)
- updated_at = db.Column(db.DateTime)
- def __init__(self, name, description):
- self.name = name
- self.description = description
- @property
- def serialize(self):
- """Return object data in easily serializable format"""
- return {
- 'id' : self.id,
- 'name' : self.name,
- 'description': self.description,
- 'created_at' : dump_datetime(self.created_at)
- }
- def __repr__(self):
- return '<Profit %r>' % self.name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement