0

I have problems operating with the exiting database in mysql using a sqlalchemy as I need it for building Flask RESTful api.

I get funny errors about circular dependencies here:

from flask import Flask, request, jsonify, make_response
from flaskext.mysql import MySQL
from flask_sqlalchemy import SQLAlchemy

#from webapi import models

# mysql = MySQL()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secretkey' #for use later
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://user:[email protected]/mydb'

db = SQLAlchemy(app)

class Extensions(db.Model):

    __tablename__ = "Extensions"

    extnbr = db.Column(db.Integer, primary_key=True, default="NULL")
    authname = db.Column(db.String(256), nullable=True, default="@nobody")
    extpriority = db.Column(db.Integer, nullable=True, default=0)
    callaccess = db.Column(db.Integer, nullable=True, default=0)
    extlocation = db.Column(db.String, nullable=True, default="NULL")
    display = db.Column(db.String, nullable=True, default="NULL")

@app.route("/")
def hello():
    return "Welcome to Python Flask App!"

@app.route("/extensions", methods=["GET"])
def get_all_extensions():
    users = Extensions.query.all()

    output = []

    for user in users:
      user_data = {}
      user_data['extnbr'] = user.extnbr
      user_data['authname'] = user.authname
      user_data['extpriority'] = user.extpriority
      user_data['callaccess'] = user.callaccess
      user_data['extlocation'] = user.extlocation
      user_data['display'] = user.display
      output.append({'extensions' : output})

    return jsonify({'users' : output})

if __name__ == "__main__":
    app.run(debug=True)

I get error about circular dependence:

Debug and trace:

https://dpaste.de/F3uX

3
  • Since you're not using from flaskext.mysql import MySQL for now, delete the line and see if it still occurs. Commented Apr 21, 2018 at 10:12
  • thx do not help I'll add the output of debug and traces. Commented Apr 21, 2018 at 10:29
  • here is a full trace of error @Joost with SQLALCHEMY_ECHO = True dpaste.de/F3uX Commented Apr 21, 2018 at 10:36

2 Answers 2

1

This has to do with your output. You're appending a dictionary containing output to output itself, creating a funky data structure causing the json error.

Try changing

 output.append({'extensions' : output})

to this:

output.append({'extensions' : user_data })

(Which is what I assume you want anyways)

Sign up to request clarification or add additional context in comments.

1 Comment

sorry how that slipped out thanks i forgot to append the dicitionary instead of the return array.
1

The problem is:

output = []

for user in users:
  ...
  output.append({'extensions' : output})  # here you are adding output to the output

check this example:

out = []
for i in range(2):
    out.append({'ext': out})
# output
[{'ext': [...]}, {'ext': [...]}]

You have the same output. I am sure this is not what you want. So change the line with problem to this one: output.append({'extensions' : user_data})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.