Python Forum
SQLAlchemy Object Missing when Null is returned
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQLAlchemy Object Missing when Null is returned
#1
I'm learning Python and SQLAlchemy for the last 2 weeks, I made some progress but I'm having an issue I have no idea how to resolve.

I do have a SQLAlchemy query as:


query = db.session.query(Inventory, BLColor, BLCategory, BLPart, Location). \
        join(BLColor, Inventory.color_id == BLColor.color_id). \
        join(BLCategory, Inventory.category_id == BLCategory.category_id). \
        join(BLPart, Inventory.part_id == BLPart.ITEMID). \
        outerjoin(Location, Inventory.location_id == Location.loc_id,full=False). \
        filter(Inventory.part_id == part_to_find, Inventory.userid == current_user.userid ).order_by(desc(Inventory.quantity))

dbparts = query.all()
Later in my code I assign a variable with the result of this query such as:
 part_form.loc = p.Location.location 
So far all works, the problem arises when the location outer join return nothing.

When everything work, when I look at the type and content of the dbparts variable, I see this

dbparts=[(<Inventory 1206>, <BLColor 86>, <BLCategory 26>, <BLPart 3020>, <Location 96>), (<Inventory 1197>, <BLColor 11>, <BLCategory 26>, <BLPart 3020>, <Location 96>), (<Inventory 1201>, <BLColor 10>, <BLCategory 26>, <BLPart 3020>, <Location 96>), (<Inventory 1211>, <BLColor 5>, <BLCategory 26>, <BLPart 3020>, <Location 96>), (<Inventory 1198>, <BLColor 7>, <BLCategory 26>, <BLPart 3020>, <Location 96>), ...
But when the location is blank I see this:

<class 'list'>
dbparts=[(<Inventory 1218>, <BLColor 11>, <BLCategory 479>, <BLPart 30200>, None]
No more <Location ###> !!! Side question how are those <Inventory 1218>, <BLColor 11> , called ?

So when I try to assign
part_form.loc = p.Location.location
Python is errors out with the error message
Error:
AttributeError: 'NoneType' object has no attribute 'location'
How do I treat this ?

I'd like to do a:
if p.Location.location exist then assign the value of part_form.loc = p.Location.location
but
if p.Location.location does not exist, set the value of part_form.loc = "UNKNOWN"
Reply
#2
If you're too far into your project you may not wish to change, but using the ORM version of SQLAlchemy is more straight forward programming wise.

I wrote a tutorial here that will show you the basics with a simple but complete package.

Also take a look at pandas which does a great job for general database operations. See post: https://python-forum.io/thread-36430.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to access values returned from inquirer cspower 6 782 Dec-26-2023, 09:34 PM
Last Post: cspower
  How to express null value klatlap 3 859 Mar-25-2023, 10:40 AM
Last Post: klatlap
  value null when update in json file 3lnyn0 6 3,222 Dec-30-2021, 05:52 PM
Last Post: ndc85430
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 3,608 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Libraries installed with pipenv, but ModuleNotFoundError returned jpncsu 2 2,998 Sep-06-2021, 07:24 PM
Last Post: jpncsu
  Multiple conditions, one is null moralear27 1 2,197 Sep-13-2020, 06:11 AM
Last Post: scidam
  I didnt get the NULL values salwa17 0 1,582 Jul-10-2020, 02:54 PM
Last Post: salwa17
  Find only the rows containing null values Bhavika 2 2,437 Jun-10-2020, 01:25 PM
Last Post: Bhavika
  Exception: Returned Type Mismatch Error devansing 1 5,148 Mar-06-2020, 07:26 PM
Last Post: ndc85430
  How to use a returned value? t4keheart 12 4,827 Jan-16-2020, 06:54 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020