Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I am trying to get a POST/rest api up with python and running into trouble.
- The current error I am getting is:
- flask run
- /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.
- 'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
- * Serving Flask app "main"
- * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
- 127.0.0.1 - - [12/Jun/2017 18:40:06] "OPTIONS /profit HTTP/1.1" 200 -
- [2017-06-12 18:40:06,127] 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).
- 127.0.0.1 - - [12/Jun/2017 18:40:06] "POST /profit HTTP/1.1" 500
- 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.
- Here is my main.py file:
- @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)
- if request.method=='GET':
- profits = [i.serialize for i in Profit.query.all()]
- return jsonify(profits)
- Here is my profits function:
- 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, default=datetime.datetime.utcnow, nullable=True)
- 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
- 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