Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. I am trying to get a POST/rest api up with python and running into trouble.
  2.  
  3. The current error I am getting is:
  4.  
  5. flask run
  6. /home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:839: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  7.   'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
  8.  * Serving Flask app "main"
  9.  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  10. 127.0.0.1 - - [12/Jun/2017 18:40:06] "OPTIONS /profit HTTP/1.1" 200 -
  11. [2017-06-12 18:40:06,127] ERROR in app: Exception on /profit [POST]
  12. Traceback (most recent call last):
  13.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
  14.     response = self.full_dispatch_request()
  15.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
  16.     rv = self.handle_user_exception(e)
  17.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
  18.     return cors_after_request(app.make_response(f(*args, **kwargs)))
  19.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
  20.     reraise(exc_type, exc_value, tb)
  21.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
  22.     rv = self.dispatch_request()
  23.   File "/home/patientplatypus/vuespring/backend_PYTHON3/env/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
  24.     return self.view_functions[rule.endpoint](**req.view_args)
  25.   File "/home/patientplatypus/vuespring/backend_PYTHON3/main.py", line 52, in profits
  26.     cur.execute(sql, params)
  27. IntegrityError: null value in column "created_at" violates not-null constraint
  28. DETAIL:  Failing row contains (1, testname, testdescription, null, null).
  29.  
  30. 127.0.0.1 - - [12/Jun/2017 18:40:06] "POST /profit HTTP/1.1" 500
  31.  
  32.  
  33.  
  34. So the integrity error tells me that I am probably having an issue with not-null (for the created at and updated at time variables). However I have made it so that they can be null in the file which I find frustrating and confusing.
  35.  
  36.  
  37. Here is my main.py file:
  38.  
  39. @app.route('/profit', methods=['POST','GET'])
  40. def profits():
  41.     if request.method=='POST':
  42.         conn = psycopg2.connect(database = "money", user = "patientplatypus", password = "Fvnjty0b")
  43.         cur = conn.cursor()
  44.         sql = "INSERT INTO PROFITS (ID,NAME,DESCRIPTION) VALUES (%s, %s, %s)"
  45.         params = (request.json['id'], request.json['name'], request.json['description'])
  46.         cur.execute(sql, params)
  47.  
  48.         conn.commit()
  49.         print "Records created successfully";
  50.         conn.close()
  51.         #
  52.         # profit = {
  53.         # 'id':request.json['id'],
  54.         # 'name':request.json['name'],
  55.         # 'description':request.json['description']
  56.         # }
  57.         # profits.append(profit)
  58.     if request.method=='GET':
  59.         profits = [i.serialize for i in Profit.query.all()]
  60.         return jsonify(profits)
  61.  
  62.  
  63. Here is my profits function:
  64.  
  65.  
  66. class Profit(db.Model):
  67.     __tablename__ = 'profits'
  68.     id = db.Column(db.Integer, primary_key=True)
  69.     name = db.Column(db.String(100), unique=True, nullable=True)
  70.     description = db.Column(db.Text)
  71.     created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=True)
  72.     updated_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=True)
  73.  
  74.     def __init__(self, name, description):
  75.         self.name = name
  76.         self.description = description
  77.  
  78.     @property
  79.     def serialize(self):
  80.         """Return object data in easily serializable format"""
  81.         return {
  82.             'id'         : self.id,
  83.             'name'       : self.name,
  84.             'description': self.description,
  85.             'created_at' : dump_datetime(self.created_at)
  86.         }
  87.  
  88.     def __repr__(self):
  89.         return '<Profit %r>' % self.name
  90.  
  91.  
  92.  
  93. I've gone ahead and remade my databases so they are clean, and therefore the nullable=True should now be a property of the columns at created at and updated at. Unfortunately it seems they are still demanding values. What should I do?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement