SQLAlchemy not creating tables with db.create_all() - python

This question has been asked a few times I never been able to fix my problem since a few days. I tried to create a new object in my sqllite db related to a Model that I created but I always have this issue:
Arguments: (OperationalError('(sqlite3.OperationalError) no such table: sku_model'),)
app.py
import requests
from flask import Flask
from flask_crontab import Crontab
from app.routes import routes_blueprint
from app.config import BaseConfig, BasicConfig, TestConfig
import os
from statistics import median
import click
from app.models import SKUModel, db
def create_app():
app = Flask(__name__)
app.config["SECRET_KEY"] = "any secret key"
app.config["SQLALCHEMY_DATABASE_URI"] = BaseConfig.SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.init_app(app)
app.register_blueprint(routes_blueprint)
crontab.init_app(app)
return app
def setup_database(app):
with app.app_context():
db.create_all()
app.logger.info("DB init!")
crontab = Crontab()
if __name__ == "__main__":
app = create_app()
# not os.path.isfile(BaseConfig.SQLALCHEMY_DATABASE_URI):
setup_database(app)
app.run()
database.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
models.py
import flask_sqlalchemy
db = flask_sqlalchemy.SQLAlchemy()
class SKUModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
sku = db.Column(db.String)
product_title = db.Column(db.String)
quantity = db.Column(db.Integer)
price = db.Column(db.Float)
# JSON serializer
def to_json(self):
return {
"id": self.id,
"sku": self.sku,
"product_title": self.product_title,
"quantity": self.quantity,
"price": self.price,
}
config.py
import os
# default config
class BaseConfig(object):
DEBUG = False
# shortened for readability
SECRET_KEY = '\xbf\xb0\x11\xb1\xcd\xf9\xba\x8bp\x0c...'
SQLALCHEMY_PATH = "/tmp/database.db"
#SQLALCHEMY_DATABASE_URI = "sqlite:////tmp/sku.db"
SQLALCHEMY_DATABASE_URI = "sqlite:///" + SQLALCHEMY_PATH
class TestConfig(BaseConfig):
DEBUG = True
TESTING = True
WTF_CSRF_ENABLED = False
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
class BasicConfig(BaseConfig):
DATASET_PATH = "app/data/dataset.json"
routes.py
from itertools import product
import json
from statistics import median
import requests
from flask import Blueprint, request
from app.models import SKUModel, db
from app.core import get, get_all, update_dataset, get_5_highest, update_21, create, delete, get_lowest
routes_blueprint = Blueprint("route_blueprint", __name__)
#routes_blueprint.route("/")
def hello():
return "Hello World!"
# Retrieve the current timezone using the WorldTimeAPI (http://worldtimeapi.org) for any country available in the service
#routes_blueprint.route("/timezone/<string:area>/<string:region>")
def timezone(area, region):
url = f"http://worldtimeapi.org/api/timezone/{area}/{region}"
response = requests.get(url)
return (
{"UTF:": response.json()["utc_offset"]}
if response.ok
else {"response error": response.text, "http code": response.status_code}
)
# Get one SKU
#routes_blueprint.route("/sku/<int:id>")
def get_sku(id):
sku = get(id)
return sku.to_json() if sku else ("Not found", 404)
# Get all SKUs
#routes_blueprint.route("/sku")
def get_all_sku():
return json.dumps([sku.to_json() for sku in get_all()])
#routes_blueprint.route("/sku/update", methods=["GET"])
def update_from_dataset():
return update_dataset()
# Get the 5 best prices for a SKU
#routes_blueprint.route("/sku/best", methods=["GET"])
def get_best_sku():
return json.dumps([sku.to_json() for sku in get_5_highest()])
# Update a SKU from an ID by increasing it's price by 21%
#routes_blueprint.route("/sku/<int:id>", methods=["PUT"])
def update_sku(id):
return update_21(id).to_json()
# Create SKU from form data
#routes_blueprint.route("/sku", methods=["POST"])
def create_sku():
sku = SKUModel(
sku=request.form["sku"],
product_title=request.form["product_title"],
quantity=request.form["quantity"],
price=request.form["price"],
)
return create(sku).to_json()
# Delete SKU from ID
#routes_blueprint.route("/sku/<int:id>", methods=["DELETE"])
def delete_sku(id):
return delete(id)
# Return the lowest price for a SKU
#routes_blueprint.route("/sku/lowest")
def get_lowest_sku():
return get_lowest().to_json()
# return median price of all SKU.
#routes_blueprint.route("/sku/median")
def get_median_sku():
return {"median": median([sku.price for sku in get_all()])}
core.py
from asyncio.log import logger
from app.models import SKUModel, db
from app.config import BasicConfig
import json
import logging
LOGGER = logging.getLogger(__name__)
def get(id):
try:
return db.session.query(SKUModel).get(id)
except Exception as e:
LOGGER.error("Error while getting SKU", e)
return None
return None
def get_all():
try:
return db.session.query(SKUModel).all()
except Exception as e:
LOGGER.error("Error while getting all SKU", e)
return None
I'm sure I missed something. I heard that db.create_all() should be use after importing my model and this is what I'm doing.

When you called db.create_all() inside app.py was imported from app.database. but the db inside of app.models is defined locally. Therefore there are no models in the db inside app.py and that is why it didn't create any tables. the simplest fix would be to update app.py and change:
from app.database import db
to
from app.models import db
running this extracted code works for me. If it doesn't for you then perhaps you have an issue writing to the /tmp/database.db file.
import flask_sqlalchemy
from flask import Flask
db = flask_sqlalchemy.SQLAlchemy()
class SKUModel(db.Model):
id = db.Column(db.Integer, primary_key=True)
sku = db.Column(db.String)
product_title = db.Column(db.String)
quantity = db.Column(db.Integer)
price = db.Column(db.Float)
SQLALCHEMY_PATH = "/tmp/database.db"
SQLALCHEMY_DATABASE_URI = "sqlite:///" + SQLALCHEMY_PATH
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
db.init_app(app)
with app.app_context():
print(db)
db.create_all()
If this simple code works, then you have to look at your code to find out why it isn't running in the expected fashion.

from models import * #Import the SQLALCHEMY object from your
#models.py i.e. db = SQLAlchemy()
db.init_app(app)
#app.before_first_request
def create_database_tables():
with app.app_context():
db.create_all()

Related

Cannot POST a JSON object to Database using Flask Python

I'm trying to post and get objects using Flask but I keep getting sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: object.
I tried doing POST into http://127.0.0.1/data
and the json I provided is
{
"oid": "123456789",
"size": "1234"
}
Below is my code
from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
db = SQLAlchemy(app)
class Object(db.Model):
oid = db.Column(db.Integer, primary_key=True)
size = db.Column(db.Integer)
def __repr__(self):
return f'Object: {self.oid}, Size: {self.size}'
#app.route('/')
def index():
return 'DSAP_2022'
#app.route('/data/<oid>', methods=['GET'])
def getObject(oid):
obj = Object.query.filter_by(oid=oid).first()
if obj is None:
return 'Object not found', 404
return repr(obj)
#app.route('/data', methods=['POST'])
def putObject():
obj = Object(oid=request.json['oid'], size=request.json['size'])
db.session.add(obj)
db.session.commit()
return 'OK'
if __name__ == '__main__':
app.run()
I am using Postman to submit my requests. and the index() works, however, both getObject and putObject doest not work because of the same error.
You're never calling db.create_all() to create the database tables. You would need something like:
from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite3'
db = SQLAlchemy(app)
class Object(db.Model):
oid = db.Column(db.Integer, primary_key=True)
size = db.Column(db.Integer)
def __repr__(self):
return f'Object: {self.oid}, Size: {self.size}'
# Create tables corresponding to sqlalchemy models
db.create_all()
#app.route('/')
def index():
return 'DSAP_2022'
#app.route('/data/<oid>', methods=['GET'])
def getObject(oid):
obj = Object.query.filter_by(oid=oid).first()
if obj is None:
return 'Object not found', 404
return repr(obj)
#app.route('/data', methods=['POST'])
def putObject():
obj = Object(oid=request.json['oid'], size=request.json['size'])
db.session.add(obj)
db.session.commit()
return 'OK'
if __name__ == '__main__':
app.run()

TypeError: <sqlalchemy.sql.elements.ColumnClause at 0x3ee9890; DATETIME> is not serializable

I'm trying to use the sqlalchemy for writing one of the GET API. Underlaying database is postgresSQL.
My datetime column is not getting json serialized. Can you please help me to resolve this issue:
Model Class:
models\rpt.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class RptModel(db.Model):
__tablename__='rpt_detail'
id = db.Column(db.Integer, primary_key=True)
user_nm = db.Column(db.VARCHAR(100), nullable=False)
created_dt = db.column(db.DateTime(timezone=True))
def __init__(self, _id, user_nm, created_dt):
self.id = _id
self.user_nm = user_nm
self.created_dt = created_dt
def json():
return {'id': self.id, 'user_nm': self.user_nm, 'created_dt': self.created_dt}
#classmethod
def get_rpt_by_id(Cls, id):
return Cls.query.filter_by(id=id).first()
Resource Class
resources\rpt.py
from flask_restful import Resource
from models.rpt import RptModel
class GetRpt(Resource):
def get(self, id):
rpt = RptModel.get_rpt_by_id(id)
if rpt:
return rpt.json(), 200
return {'message': 'rpt not found'}, 404
app.py
from flask import Flask
from flask_restful import Api
from resources.rpt import GetRpt
app = Flask(__name__)
app.config['POSTGRESQL_USER'] = 'AAAA'
app.config['POSTGRESQL_PASSWORD'] = 'BBBB'
app.config['POSTGRESQL_HOST'] = 'CCCC.DDD.COM'
app.config['POSTGRESQL_DB'] = 'EEEE'
postgres_db_uri = 'postgresql://'+app.config['POSTGRESQL_USER']+':'+app.config['POSTGRESQL_PASSWORD']+'#'+app.config['POSTGRESQL_HOST']+'/'+app.config['POSTGRESQL_DB']
app.config['SQLALCHEMY_DATABASE_URI'] = postgres_db_uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
api = APi(app)
api.add_resource(GetRpt, 'rpt/<string:id>')
if __name__='__main__':
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
db.init_app(app)
app.run(debug=True)
Postgres RPT Table
Table: rpt_detail
column Type Collation Nullable Default
id integer not null nextval('rpt_detail_id_seq'::regclass)
user_nm character varying(100) not null
created_dt timestamp with time zone not null
When I'm trying to hit this from the postman, I'm getting error stating
TypeError: <sqlalchemy.sql.elements.ColumnClause at 0x3ee9890; DATETIME> is not JSON serializable
I'm not able to convert the created_dt field to JSON format. Can anyone provide any resolution to this.

How to get SQLAlchemy working with mysql with existing database

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:password#127.0.0.1/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
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)
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})

Create new session for every request for Flask-Sqlalchemy

I have two servers behind a load balancer. If a PUT or POST transaction is being run on one server, the other server will not be able to query the latest data i.e.
Request 1: Change vehicle's name, for id 2, from Car 001 to Car 101.
Request 2: Get vehicle's name for id 2 and it will show Car 001 until a few minutes then it will change to Car 101.
My folder structure is like this:
-api
--helpers
--models
---init.py
---vehicle.py
--resources
---init.py
---vehicles.py
Here is the code for init.py in models:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "url"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_POOL_RECYCLE'] = 5
app.config['SQLALCHEMY_POOL_SIZE'] = 10
db = SQLAlchemy(app)
ma = Marshmallow(app)
Here is the vehicle model:
import arrow, json, uuid
import jwt
from datetime import datetime
import datetime as dt
from functools import wraps
from api.models import db, ma
from api.titan.v1.models.vehicle_state import VehicleState
class Vehicle(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(300), index=True)
createdAt = db.Column(db.DateTime)
plateNumber = db.Column(db.String(300))
Here is the vehicle requests handling:
import arrow, json, uuid, pytz
import requests
from datetime import datetime
from flask_restful import reqparse, abort, Api, Resource
from werkzeug.exceptions import BadRequest
from flask import request, jsonify
from api.models import db
class Vehicles(Resource):
def get(self, vehicle_id=None):
response_object = {
'object': 'vehicle',
'verb': 'GET',
'timestamp': arrow.utcnow().timestamp
}
if vehicle_id is None:
vehicles = db.session.query(Vehicle).filter_by(companyId=user['companyId']).all()
response_object['data'] = [schema.dump(i).data for i in vehicles]
else:
vehicle = db.session.query(Vehicle).filter_by(id=vehicle_id, companyId=user['companyId']).first()
response_object['data'] = schema.dump(vehicle).data
return response_object, 200
def put(self, vehicle_id):
data = request.get_json()
response_object = {
'object': 'vehicle',
'verb': 'PUT',
'timestamp': arrow.utcnow().timestamp,
}
vehicle = db.session.query(Vehicle).filter_by(id=vehicle_id).first()
vehicle.name = data['name']
db.session.commit()
response_object['data'] = vehicle
return response_object, 200

OperationalError: (sqlite3.OperationalError) no such table: user

I'm completely new to flask and web development in general. And what I need is to login to a website using steam id. I'm doing it as it said here, but get the following error:
OperationalError: (sqlite3.OperationalError) no such table: user
It seems to open up steam website correctly but it breaks when I press Log In. So, what's my mistake ? Any help is appreciated.
The code:
from flask import Flask, render_template, redirect, session, json, g
from flask_bootstrap import Bootstrap
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.openid import OpenID
import urllib
import re
app = Flask(__name__)
app.secret_key = '123'
Bootstrap(app)
app.config.from_pyfile('settings.cfg')
db = SQLAlchemy(app)
oid = OpenID(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
steam_id = db.Column(db.String(40))
nickname = db.String(80)
#staticmethod
def get_or_create(steam_id):
rv = User.query.filter_by(steam_id=steam_id).first()
if rv is None:
rv = User()
rv.steam_id = steam_id
db.session.add(rv)
return rv
def get_steam_userinfo(steam_id):
options = {
'key': app.config['STEAM_API_KEY'],
'steamids': steam_id
}
url = 'http://api.steampowered.com/ISteamUser/' \
'GetPlayerSummaries/v0001/?%s' % urllib.urlencode(options)
rv = json.load(urllib.urlopen(url))
return rv['response']['players']['player'][0] or {}
_steam_id_re = re.compile('steamcommunity.com/openid/id/(.*?)$')
#app.route('/login')
#oid.loginhandler
def login():
if g.user is not None:
return redirect(oid.get_next_url())
return oid.try_login('http://steamcommunity.com/openid')
#oid.after_login
def create_or_login(resp):
match = _steam_id_re.search(resp.identity_url)
g.user = User.get_or_create(match.group(1))
steamdata = get_steam_userinfo(g.user.steam_id)
g.user.nickname = steamdata['personaname']
db.session.commit()
session['user_id'] = g.user.id
flash('You are logged in as %s' % g.user.nickname)
return redirect(oid.get_next_url())
#app.before_request
def before_request():
g.user = None
if 'user_id' in session:
g.user = User.query.get(session['user_id'])
#app.route('/')
def homepage():
return render_template('mainpage.html')
#app.route('/logout')
def logout():
session.pop('user_id', None)
return redirect(oid.get_next_url())
if __name__ == '__main__':
app.run(debug=True)
You need to run a db.create_all() before running your app.
This will create all the tables described by your model in the database.
If you are new to flask you can follow the quickstart quide here

Categories

Resources