Sqlalchemy relationship issue working with datatables - datatables

I'm currently struggling creating web tables using bootstrap datatables, sqlalchemy and sqlalchemy-datatables.
Sqlalchemy seems generating correct sql query, datatables is populated with correct information.
However when I'm trying to search for record in datatable search field I'm getting an error:
DataTables warning: table id=main_table - Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with VrfMain.scope has an attribute 'cast'
I tried to remove any relationships from query, and it works.
So problem is with relationship somewhere. Can anyone help me please ?
Here is my sql models:
class VrfMain(db.Model):
__tablename__ = 'vrf_main'
id = Column(Integer, primary_key=True, autoincrement=True)
vrf_name = Column(String, unique=True)
rd = Column(String, unique=True)
primary_rt = Column(String, unique=True)
additional_rt = Column(String, unique=True)
description = Column(String)
scope_id = Column(Integer, ForeignKey('subnet_scopes.id'))
scope = relationship('SubnetScopes')
def __init__(self, vrf_name, rd, primary_rt, description, scope_id):
self.vrf_name = vrf_name
self.rd = rd
self.primary_rt = primary_rt
self.description = description
self.scope_id = scope_id
class SubnetScopes(db.Model):
__tablename__ = 'subnet_scopes'
id = Column(Integer, primary_key=True, autoincrement=True)
scope_name = Column(String, unique=True)
def __init__(self, scope_name):
self.scope_name = scope_name
def __repr__(self):
return str(self.scope_name)
Here is part of flask code:
# defining datatable columns
columns = [
ColumnDT(VrfMain.id),
ColumnDT(VrfMain.vrf_name),
ColumnDT(VrfMain.rd),
ColumnDT(VrfMain.primary_rt),
ColumnDT(VrfMain.additional_rt),
ColumnDT(VrfMain.description),
ColumnDT(VrfMain.scope)
]
query = VrfMain.query.\
join(SubnetScopes).\
filter(VrfMain.scope_id == SubnetScopes.id).\
with_entities(VrfMain.id, VrfMain.vrf_name, VrfMain.rd, VrfMain.primary_rt, VrfMain.additional_rt, VrfMain.description, SubnetScopes.scope_name)
print(query)
params = request.args.to_dict()
rowTable = DataTables(params, query, columns)
return jsonify(rowTable.output_result())
Here is sql query that is generated
SELECT vrf_main.id AS vrf_main_id, vrf_main.vrf_name AS vrf_main_vrf_name, vrf_main.rd AS vrf_main_rd, vrf_main.primary_rt AS vrf_main_primary_rt, vrf_main.additional_rt AS vrf_main_additional_rt, vrf_main.description AS vrf_main_description, subnet_scopes.scope_name AS subnet_scopes_scope_name
FROM vrf_main INNER JOIN subnet_scopes ON subnet_scopes.id = vrf_main.scope_id
WHERE vrf_main.scope_id = subnet_scopes.id
Here is javascript code:
$(document).ready(function() {
var table = $('#main_table').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "{{ url_for('home_blueprint.get_vrf_data') }}"
},
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],

I decide not to use sqlalchemy-datatables. instead I'm will use code:
all_vrf_data = []
for row in VrfMain.query.all():
row_proccessed = dict(row.__dict__); row_proccessed.pop('_sa_instance_state', None)
all_vrf_data.append(row_proccessed)
return_data = {"data": all_vrf_data}
return json.dumps(return_data, indent=4, sort_keys=True, default=str)

Related

ArgumentError: Mapper mapped class Roles_users->User Roles could not assemble any primary key columns for mapped table 'User Roles'

I'm trying to write a web app with user roles and I keep getting this error no matter the changes I make.
Could you please help me find what's wrong?
Thank you in advance!!
This is my models.py file:
from sqlalchemy import Column, Integer, String, Float, Boolean, ForeignKey
from flask_login import UserMixin
from sqlalchemy.orm import relationship, backref
import db
class Rol(db.Base):
__tablename__ = 'Roles'
id_rol = Column(Integer, primary_key=True)
nombre_rol = Column(String, nullable=False)
user_rol = relationship("User rol", back_populates="rol", uselist =False)
def __init__(self, nombre_rol):
self.nombre_rol = nombre_rol
class User(UserMixin, db.Base):
__tablename__ = 'Users'
user_id = Column(Integer, primary_key=True)
username = Column(String, unique=True, nullable=False)
password = Column(String, unique=True, nullable=False)
is_active = Column(Boolean, default=False)
user_rol = relationship("User rol", back_populates="user", uselist =False)
def __init__(self,username,password,is_active):
self.username = username
self.password = password
self.is_active = is_active
def get_id(self):
return self.id
def is_active(self):
return self.is_active
def activate_user(self):
self.is_active = True
def get_username(self):
return self.username
def get_rol(self):
return self.rol
class Roles_users(db.Base):
__tablename__ = 'User Roles'
user_id = Column(Integer, ForeignKey("Users.user_id"))
id_rol = Column(Integer, ForeignKey("Roles.id_rol"))
user = relationship("User", back_populates="user roles")
rol = relationship("Rol", back_populates="user roles")

Issue when creating on-to-many relationships in SQLAlchemy database

I created a Flask app with a database, with following classes:
db = SQLAlchemy(app)
class Category(db.Model):
__tablename__ = 'Category'
children = relationship("Child")
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.Text())
icon = db.Column(db.Text())
subcategories = db.relationship('Subcategory', backref="category")
def __init__(self, name, subcategories, icon):
self.name = name
self.color = icon
self.subcategories = subcategories
class Subcategory(db.Model):
__tablename__ = 'Subcategory'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.Text())
color = db.Column(db.Text())
reward_points = db.Column(db.Integer())
category = db.Column(db.Text())
tasks = db.relationship('Task', backref="subcategory")
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
def __init__(self, name, color, reward_points, category, tasks, category_id):
self.name = name
self.color = color
self.reward_points = reward_points
self.category = category
self.tasks = tasks
self.category_id = category_id
class Task(db.Model):
__tablename__ = 'Task'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
task = db.Column(db.Text())
start = db.Column(db.DateTime())
end = db.Column(db.DateTime())
duration = db.Column(db.Integer)
scheduled = db.Column(db.DateTime())
created = db.Column(db.DateTime())
status = db.Column(db.Text())
category = db.Column(db.Text())
subcategory = db.Column(db.Text())
tags = db.Column(db.Text())
subcategory_id = db.Column(db.Integer, db.ForeignKey('subcategory.id'))
def __init__(self, task, start, end, duration, category, subcategory, tags, created, status, scheduled, subcategory_id):
self.task = task
self.start = start
self.end = end
self.duration = duration
self.category = category
self.subcategory = subcategory
self.tags = tags
self.created = created
self.status = status
self.scheduled = scheduled
self.subcategory_id = subcategory_id
My goal is to create one to many relationships between the classes. I am trying to run the db.create_all() command, but am getting following error:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'Subcategory.category_id' could not find table 'category' with which to generate a foreign key to target column 'id'
What am I doing wrong? Other questions on Stackoverflow with similar issues did not resolve my error.
Changing
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
to
category_id = db.Column(db.Integer, db.ForeignKey('Category.id'))
solved the issue.
Changing "category.id" to "category.c.id" solved the issue as well. If anyone knows why, pls let me know ;-)

Convert PostgreSQL COUNT … FILTER query to SQL Alchemy

I'm new to SQLAlchemy, and I would like to convert this PostgreSQL query:
SELECT product.*
, COUNT(feedback.like) FILTER (WHERE feedback.like = '1') AS like
, COUNT(feedback.like) FILTER (WHERE feedback.like = '-1') AS unlike
FROM feedback, product
WHERE product.id = feedback.product_id
GROUP BY product.id
ORDER BY product.id;
I have already tried this:
products = db.session.query(
Product,
func.count(Feedback.like > 0).label('like'),
func.count(Feedback.like < 0).label('unlike')
).filter(Product.guide_name_id==id)
.filter(Product.id == Feedback.product_id)
.group_by(Product.id)
.order_by(Product.id)
.all()
Thank you in advance for your help
Thanks to #IljaEverilä's comment, here is a more direct answer:
class Product(Base):
__tablename__ = "product"
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
def __repr__(self):
return f"<Product(name='{self.name}')>"
class Feedback(Base):
__tablename__ = "feedback"
id = Column(Integer, primary_key=True)
product_id = Column(Integer, ForeignKey(Product.id))
like = Column(Integer)
product = relationship(Product)
Base.metadata.create_all(engine)
with Session(engine) as session:
# set up test data
widget = Product(name="widget")
session.add_all(
[
widget,
Feedback(product=widget, like=1),
Feedback(product=widget, like=1),
Feedback(product=widget, like=-1),
Product(name="gadget"),
]
)
# run the query
query = (
select(
Product,
func.count(Feedback.like)
.filter(Feedback.like == 1)
.label("likes"),
func.count(Feedback.like)
.filter(Feedback.like == -1)
.label("dislikes"),
)
.select_from(Product)
.outerjoin(Feedback)
.group_by(Product)
)
results = session.execute(query).fetchall()
print(results)
# [(<Product(name='gadget')>, 0, 0), (<Product(name='widget')>, 2, 1)]
(Original answer)
I'm not sure if SQLAlchemy's postgresql dialect specifically handles COUNT … FILTER, but you can accomplish the same thing using SUM and CASE:
from sqlalchemy import __version__ as sa_version, case, Column, ForeignKey, func, Integer, String
from sqlalchemy.orm import Session
print(sa_version) # 1.4.0b2
class Product(Base):
__tablename__ = "product"
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
class Feedback(Base):
__tablename__ = "feedback"
id = Column(Integer, primary_key=True)
product_id = Column(Integer, ForeignKey(Product.id))
like = Column(Integer)
product = relationship(Product)
Base.metadata.create_all(engine)
with Session(engine, future=True) as session:
widget = Product(name="widget")
session.add_all(
[
widget,
Feedback(product=widget, like=1),
Feedback(product=widget, like=1),
Feedback(product=widget, like=-1),
Product(name="gadget"),
]
)
results = (
session.query(
Product.name,
func.sum(case((Feedback.like > 0, 1), else_=0)).label(
"likes"
),
func.sum(case((Feedback.like < 0, 1), else_=0)).label(
"dislikes"
),
)
.select_from(Product)
.outerjoin(Feedback)
.group_by(Product)
.all()
)
print(results) # [('widget', 2, 1), ('gadget', 0, 0)]

SqlAlchemy outerjoin query problem filtering [duplicate]

This question already has answers here:
how left outer join in sqlalchemy?
(1 answer)
sqlalchemy filter children in query, but not parent
(2 answers)
Closed 2 years ago.
I'm using sqlalchemy, and I have problem with this specific query.
I have data_template, devices_data, and device. Each device have value for each data in data_template. Those values are stored in devices_data. I want to list data_template for one device with values that this device has. If there is no value for some data_template, show None.
It has something to do with outerjoin. Here is my model:
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class DataTemplate(Base):
__tablename__ = 'data_template'
id = Column(Integer, primary_key=True)
name = Column(String)
def __repr__(self):
return f"<DataTemplate(name={self.name})>"
class Device(Base):
__tablename__ = 'device'
id = Column(Integer, primary_key=True)
name = Column(String)
def __repr__(self):
return f"<Device(name={self.name})>"
class DeviceData(Base):
__tablename__ = 'device_data'
id = Column(Integer, primary_key=True)
value = Column(Integer, nullable=False)
data_name_id = Column(Integer, ForeignKey(DataTemplate.id), nullable=False)
device_id = Column(Integer, ForeignKey(Device.id), nullable=False)
data_template = relationship('DataTemplate', backref='device_data')
device = relationship('Device', backref='device_data')
def __repr__(self):
return f"<DeviceData(device={self.device.name}, data_template={self.data_template.name}, value={self.value})>"
engine = create_engine('sqlite://')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
Session.configure(bind=engine)
session = Session()
dev1 = Device(name='Receiver')
dev2 = Device(name='TV')
dat_temp1 = DataTemplate(name="height")
dat_temp2 = DataTemplate(name="width")
dat_temp3 = DataTemplate(name="length")
session.add_all([dev1, dev2, dat_temp1, dat_temp2, dat_temp3])
dd1 = DeviceData(value=100, data_template=dat_temp1, device=dev1)
dd2 = DeviceData(value=50, data_template=dat_temp2, device=dev1)
dd3 = DeviceData(value=200, data_template=dat_temp1, device=dev2)
dd4 = DeviceData(value=40, data_template=dat_temp2, device=dev2)
dd5 = DeviceData(value=30, data_template=dat_temp3, device=dev2)
session.add_all([dd1, dd2, dd3, dd4, dd5])
s = session.query(DataTemplate, DeviceData).outerjoin(DeviceData).filter(DeviceData.device==dev1)
for x in s:
print(x)
with this outerjoin I'm getting:
(<DataTemplate(name=height)>, <DeviceData(device=Receiver, data_template=height, value=100)>)
(<DataTemplate(name=width)>, <DeviceData(device=Receiver, data_template=width, value=50)>)
and is equal to:
SELECT "d"."id", "val"."id"
FROM "DataTemplate" "d"
LEFT JOIN "DeviceData" "val"
ON "d"."id" = "val"."data_name_id"
WHERE "val"."device_id" = 1
but I want to get:
(<DataTemplate(name=height)>, <DeviceData(device=Receiver, data_template=height, value=100)>)
(<DataTemplate(name=width)>, <DeviceData(device=Receiver, data_template=width, value=50)>)
(<DataTemplate(name=length)>, None)
and that query should be:
SELECT "d"."id", "val"."id"
FROM "DataTemplate" "d"
LEFT JOIN "DeviceData" "val"
ON "d"."id" = "val"."data_name_id" AND "val"."device_id" = 1
how do I write this specific query?

SQL Alchemy query multiple tables

I am trying to make a query using SQL alchemy which would otherwise been very simple if I wasn't using ORM, so I am thinking to myself surely there must be a straightforward way. I have gone through most of the questions on this topic but don't seem to answer my question. I have these two tables
class Artisan(Base):
__tablename__ = 'artisan'
name = Column(String(80), nullable=False)
skill = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)
bio = Column(String(300))
category = Column(Integer, ForeignKey(Category.id))
user = Column(Integer, ForeignKey(Users.id))
id_no = Column(Integer, nullable=False)
users = relationship(Users)
and
class Address(Base):
__tablename__ = 'address'
building = Column(String(80), nullable=False)
floor = Column(String(80), nullable=False)
house_no = Column(String(80), nullable=False)
telephone = Column(String(80), nullable=False)
kwetu_address = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)
lat = Column(String(25))
lng = Column(String(25))
artisan = Column(Integer, ForeignKey(Artisan.id))
user = Column(Integer, ForeignKey(Users.id))
users = relationship(Users)
I would like to filter the Artisans by a category, then filter the addresses by the filtered artisans, and present the results in a way that associates the artisan with respective addresses hopefully getting sqlachemy to do the filtering
The best I can come up with involves two queries and post processing which I feel is very inefficient
my_artisans = (session.query(Artisan).filter_by(category=cat_id))
my_addresses = (session.query(Address)
.join(Artisan, Artisan.id ==Address.artisan).filter_by(category=cat_id))
return jsonify(artisans =[art.serialize for art in my_artisans], addresses=[add.serialize for add in my_addresses])
Thanks
Add on - all related classes
import sys
from sqlalchemy import Column, ForeignKey, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
Base = declarative_base()
class Users(Base):
__tablename__ = 'users'
name = Column(String(80), nullable=False)
email = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)
bio = Column(String(300))
picture = Column(String(80))
class Category(Base):
__tablename__ = 'category'
name = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)
user = Column(Integer, ForeignKey(Users.id))
users = relationship(Users)
#property
def serialize(self):
return{
'id': self.id,
'name': self.name
}
class Artisan(Base):
__tablename__ = 'artisan'
name = Column(String(80), nullable=False)
skill = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)
bio = Column(String(300))
category = Column(Integer, ForeignKey(Category.id))
user = Column(Integer, ForeignKey(Users.id))
id_no = Column(Integer, nullable=False)
users = relationship(Users)
#property
def serialize(self):
return{
'id': self.id,
'name': self.name,
'skill': self.skill,
'category': self.category,
'bio': self.bio,
'id_no': self.id_no
}
class Portfolio(Base):
__tablename__ = 'portfolio'
title = Column(String(80), nullable=False)
details = Column(String(300), nullable=False)
id = Column(Integer, primary_key=True)
artisan = Column(Integer, ForeignKey(Artisan.id))
user = Column(Integer, ForeignKey(Users.id))
users = relationship(Users)
#property
def serialize(self):
return{
'id': self.id,
'title': self.title,
'details': self.details
}
class Endorsements(Base):
__tablename__ = 'endorsements'
title = Column(String(80), nullable=False)
details = Column(String(300), nullable=False)
id = Column(Integer, primary_key=True)
artisan = Column(Integer, ForeignKey(Artisan.id))
user = Column(Integer, ForeignKey(Users.id))
users = relationship(Users)
#property
def serialize(self):
return{
'id': self.id,
'title': self.title,
'details': self.details
}
class Address(Base):
__tablename__ = 'address'
building = Column(String(80), nullable=False)
floor = Column(String(80), nullable=False)
house_no = Column(String(80), nullable=False)
telephone = Column(String(80), nullable=False)
kwetu_address = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)
lat = Column(String(25))
lng = Column(String(25))
artisan = Column(Integer, ForeignKey(Artisan.id))
user = Column(Integer, ForeignKey(Users.id))
users = relationship(Users)
#property
def serialize(self):
return{
'id': self.id,
'lat': self.lat,
'lng': self.lng,
'kwetu_address': self.kwetu_address,
'artisan': self.artisan
}
engine = create_engine('sqlite:///mycatalog.db')
Base.metadata.create_all(engine)
This gave me what I needed in terms of associating my artisan with respective addresses, though still using two queries
my_artisans = (session.query(Artisan).filter_by(category=cat_id))
my_addresses = (session.query(Address)
.join(Artisan, Artisan.id ==Address.artisan).filter_by(category=cat_id))
these_addresses = []
for art in my_artisans:
art_id = art.id
for add in my_addresses:
if art_id == add.artisan:
grouped_address = {"Artisan Id" : art.id, "name" : art.name, "skill" : art.skill, "Lat" : add.lat, "lng" : add.lng}
these_addresses.append(grouped_address)
return jsonify({'Addresses': these_addresses })