Skip to main content

KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS' , I keep getting this error even after trying the solution to previous posts

I was trying to make a dashboard that would have an API for retrieving data from the database then app.py would have all the code for analytics and then I would have designed the dashboard. The problem is I was following this tutorial for APIs and everything was going well and then I got this error, I referred to the answers to similar errors in StackOverflow but couldn't make much sense.

http://192.168.224.111:5000/dataapi/jsondata

This is the address which when loaded is causing this error

Here is the code:-

dataapi.py

from flask import Flask, request, jsonify 
from flask_sqlalchemy import SQLAlchemy 
from flask_marshmallow import Marshmallow 
from flask_restful import Resource, Api


dataapi = Flask(__name__) 
api = Api(dataapi) 
dataapi.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///jsondata.db' 
dataapi.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 
db = SQLAlchemy(dataapi) 
ma = Marshmallow(dataapi)

class Data(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    end_year = db.Column(db.String(32))
    intensity = db.Column(db.String(32))
    sector = db.Column(db.String(32))
    topic = db.Column(db.String(32))
    insight = db.Column(db.Integer)
    url = db.Column(db.String(1000))
    region = db.Column(db.String(50))
    start_year = db.Column(db.String(50))
    impact = db.Column(db.String(50))
    added = db.Column(db.String(50))
    published = db.Column(db.String(50))
    country = db.Column(db.String(50))
    relevance = db.Column(db.Integer)
    pestle = db.Column(db.String(50))
    source = db.Column(db.String(50))
    title = db.Column(db.String(100))
    likelihood = db.Column(db.Integer)
    
    def __init__(self, end_year, intensity, sector, topic, insight, url, region, start_year, impact, added, published, country, relevance, pestle, source, title, likelihood):
        self.end_year = end_year
        self.intensity = intensity
        self.sector = sector
        self.topic = topic
        self.insight = insight
        self.url = url
        self.region = region
        self.start_year = start_year
        self.impact = impact
        self.added = added
        self.published = published
        self.country = country
        self.relevance = relevance
        self.pestle = pestle
        self.source = source
        self.title =title
        self.likelihood =likelihood


class DataSchema(ma.Schema):
    class Meta:
        fields = ('id', 'end_year', 'intensity', 'sector', 'topic', 'insight', 'url', 'region', 'start_year', 'impact', 'added', 'published', 'country', 'relevance', 'pestle', 'source', 'title', 'liklihood')

db.create_all()

data_schema = DataSchema() 
datas_schema = DataSchema(many=True)

class DataManager(Resource): 
    
    @staticmethod
    def get():
        try: id = request.args['id']
        except Exception as _: id = None

        if not id:
            datas = Data.query.all()
            return jsonify(datas_schema.dump(datas))
        data = Data.query.get(id)
        return jsonify(data_schema.dump(data))

    @staticmethod
    def post():
        k = 0
        try:
            while True:
                end_year = request.json[k]['end_year']
                intensity = request.json[k]['intensity']
                sector = request.json[k]['sector']
                topic = request.json[k]['topic']
                insight = request.json[k]['insight']
                url = request.json[k]['url']
                region = request.json[k]['region']
                start_year = request.json[k]['start_year']
                impact = request.json[k]['impact']
                added = request.json[k]['added']
                published = request.json[k]['published']
                country = request.json[k]['country']
                relevance = request.json[k]['relevance']
                pestle = request.json[k]['pestle']
                source = request.json[k]['source']
                title = request.json[k]['title']
                likelihood = request.json[k]['likelihood']
                data = Data(end_year, intensity, sector, topic, insight, url, region, start_year, impact, added, published, country, relevance, pestle, source, title, likelihood)
                db.session.add(data)
                db.session.commit()
                k+=1

        except: return jsonify({
                'Message': f'data inserted.'
            })

    @staticmethod
    def put():
        try: id = request.args['id']
        except Exception as _: id = None

        if not id:
            return jsonify({ 'Message': 'Must provide the data ID' })

        data = Data.query.get(id)
        end_year = request.json['end_year']
        intensity = request.json['intensity']
        sector = request.json['sector']
        topic = request.json['topic']
        insight = request.json['insight']
        url = request.json['url']
        region = request.json['region']
        start_year = request.json['start_year']
        impact = request.json['impact']
        added = request.json['added']
        published = request.json['published']
        country = request.json['country']
        relevance = request.json['relevance']
        pestle = request.json['pestle']
        source = request.json['source']
        title = request.json['title']
        likelihood = request.json['likelihood']

        data.end_year = end_year
        data.intensity = intensity
        data.sector = sector
        data.topic = topic
        data.insight = insight
        data.url = url
        data.region = region
        data.start_year = start_year
        data.impact = impact
        data.added = added
        data.published = published
        data.country = country
        data.relevance = relevance
        data.pestle = pestle
        data.source = source
        data.title = title
        data.likelihood = likelihood

        db.session.commit()
        return jsonify({
            'Message': f'data altered.'
        })

    @staticmethod
    def delete():
        try: id = request.args['id']
        except Exception as _: id = None

        if not id:
            return jsonify({ 'Message': 'Must provide the data ID' })

        data = Data.query.get(id)
        db.session.delete(data)
        db.session.commit()

        return jsonify({
            'Message': f'Data {str(id)} deleted.'
        })


api.add_resource(DataManager, '/dataapi/jsondata')

if __name__ == '__main__':
    dataapi.run(debug=True)

app.py

from flask import render_template
import connexion

app = connexion.App(__name__, specification_dir="./")
app.add_api("swagger.yml")

@app.route("/")
def home():
    return render_template("home.html")

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)

swagger.yml

openapi: 3.0.0
info:
  title: "API to return Data"
  description: "this is the api which dels with the data being used to create the visalization"
  version: "1.0.0"

servers:
  - url: "/dataapi"

paths:
  /jsondata:
    get:
      operationId: "dataapi.DataManager.get"
      tags:
        - "Data"
      summary: "Read the data in the file"
      responses:
        "200":
          description: "Successfully read the data"

home.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello world</title>
</head>
<body>
    Hello World
</body>
</html>

Just in case here is the data in the database

{
            "end_year": "",
            "intensity": 6,
            "sector": "Energy",
            "topic": "gas",
            "insight": "Annual Energy Outlook",
            "url": "http://www.eia.gov/outlooks/aeo/pdf/0383(2017).pdf",
            "region": "Northern America",
            "start_year": "",
            "impact": "",
            "added": "January, 20 2017 03:51:25",
            "published": "January, 09 2017 00:00:00",
            "country": "United States of America",
            "relevance": 2,
            "pestle": "Industries",
            "source": "EIA",
            "title": "U.S. natural gas consumption is expected to increase during much of the projection period.",
            "likelihood": 3
        },
        {
            "end_year": "",
            "intensity": 6,
            "sector": "Energy",
            "topic": "oil",
            "insight": "Annual Energy Outlook",
            "url": "http://www.eia.gov/outlooks/aeo/pdf/0383(2017).pdf",
            "region": "Northern America",
            "start_year": "",
            "impact": "",
            "added": "January, 20 2017 03:51:24",
            "published": "January, 09 2017 00:00:00",
            "country": "United States of America",
            "relevance": 2,
            "pestle": "Industries",
            "source": "EIA",
            "title": "Reference case U.S. crude oil production is projected to recover from recent declines.",
            "likelihood": 3
        },
        {
            "end_year": "",
            "intensity": 6,
            "sector": "Energy",
            "topic": "consumption",
            "insight": "Annual Energy Outlook",
            "url": "http://www.eia.gov/outlooks/aeo/pdf/0383(2017).pdf",
            "region": "Northern America",
            "start_year": "",
            "impact": "",
            "added": "January, 20 2017 03:51:23",
            "published": "January, 09 2017 00:00:00",
            "country": "United States of America",
            "relevance": 2,
            "pestle": "Industries",
            "source": "EIA",
            "title": "U.S. petroleum consumption is projected to remain below the 2005 level.",
            "likelihood": 3
        }

Please help me figure out why I am having this error and also pls suggest any modification with my code.



source https://stackoverflow.com/questions/76274057/keyerror-sqlalchemy-track-modifications-i-keep-getting-this-error-even-afte

Comments

Popular posts from this blog

Prop `className` did not match in next js app

I have written a sample code ( Github Link here ). this is a simple next js app, but giving me error when I refresh the page. This seems to be the common problem and I tried the fix provided in the internet but does not seem to fix my issue. The error is Warning: Prop className did not match. Server: "MuiBox-root MuiBox-root-1" Client: "MuiBox-root MuiBox-root-2". Did changes for _document.js, modified _app.js as mentioned in official website and solutions in stackoverflow. but nothing seems to work. Could someone take a look and help me whats wrong with the code? Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console