Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1. I'm trying to get a simple POST/rest api off the ground and am getting the following error:
  2.  
  3. 2017-06-12 18:14:51,466] ERROR in app: Exception on /profit [POST]
  4. Traceback (most recent call last):
  5.  File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
  6.    response = self.full_dispatch_request()
  7.  File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
  8.    rv = self.handle_user_exception(e)
  9.  File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
  10.    return cors_after_request(app.make_response(f(*args, **kwargs)))
  11.  File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
  12.    reraise(exc_type, exc_value, tb)
  13.  File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
  14.    rv = self.dispatch_request()
  15.  File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
  16.    return self.view_functions[rule.endpoint](**req.view_args)
  17.  File "/home/patientplatypus/vuespring/backend_PYTHON3/main.py", line 52, in profits
  18.    cur.execute(sql, params)
  19. IntegrityError: null value in column "created_at" violates not-null constraint
  20. DETAIL:  Failing row contains (1, testname, testdescription, null, null).
  21.  
  22.  
  23.  
  24.  
  25. So when I post I call this from main:
  26.  
  27. app.route('/profit', methods=['POST','GET'])
  28. def profits():
  29.    if request.method=='POST':
  30.        conn = psycopg2.connect(database = "money", user = "patientplatypus", password = "Fvnjty0b")
  31.        cur = conn.cursor()
  32.        sql = "INSERT INTO PROFITS (ID,NAME,DESCRIPTION) VALUES (%s, %s, %s)"
  33.        params = (request.json['id'], request.json['name'], request.json['description'])
  34.        cur.execute(sql, params)
  35.  
  36.        conn.commit()
  37.        print "Records created successfully";
  38.        conn.close()
  39.        #
  40.        # profit = {
  41.        # 'id':request.json['id'],
  42.        # 'name':request.json['name'],
  43.        # 'description':request.json['description']
  44.        # }
  45.        # profits.append(profit)
  46.  
  47.  
  48. 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?
  49.  
  50.  
  51.  
  52. This is my profits.py file that is breaking
  53.  
  54.  
  55.  
  56.  
  57.  
  58. from shared import db, dump_datetime
  59. import datetime
  60.  
  61.  
  62. class Profit(db.Model):
  63.    __tablename__ = 'profits'
  64.    id = db.Column(db.Integer, primary_key=True)
  65.    name = db.Column(db.String(100), unique=True, nullable=True)
  66.    description = db.Column(db.Text)
  67.    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=True)
  68.    updated_at = db.Column(db.DateTime)
  69.  
  70.    def __init__(self, name, description):
  71.        self.name = name
  72.        self.description = description
  73.  
  74.    @property
  75.    def serialize(self):
  76.        """Return object data in easily serializable format"""
  77.        return {
  78.            'id'         : self.id,
  79.            'name'       : self.name,
  80.            'description': self.description,
  81.            'created_at' : dump_datetime(self.created_at)
  82.        }
  83.  
  84.    def __repr__(self):
  85.        return '<Profit %r>' % self.name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement