0

I'm trying to use SQLAlchemy to extract certain data. In this case, its for obtaining the age from a list of peopls stored in the "Profiles" class. However, when doing so, I get an error stating that: 'map' object has no attribute 'query'. I've tried doing this various other ways but it still results with an attribute error of sorts which makes me believe I'm not properly connecting or setting it up right?

profile_db = map(
  lambda uri: sessionmaker(bind=create_engine(uri), autoflush=True)(),
  [PROFILE_URI]
)

class pull_age(object):
  def on_get(self, req, res):
    person_age = profile_db.query(Profile).filter(Profile.id == req).first
    res.json = {'age of person': person_age}
5
  • Is this Python 3+? Commented Jul 10, 2018 at 0:06
  • yeah, Python 3.6.5 to be exact Commented Jul 10, 2018 at 0:08
  • In Python 3, map returns back a map object not a list. You'd need to wrap your map call with list to covert back to a list. Though now, you'd be calling the query method on a list? Commented Jul 10, 2018 at 0:10
  • Is there an alternate way you'd recommend that I could use to query the database of Profiles? When I wrap it in the list I get AttributeError: 'NoneType' object has no attribute '_instantiate_plugins' Commented Jul 10, 2018 at 0:21
  • Wrapping as in list(map(lambda ... )). I'm not so familiar with sqlalchemy, but my suspicion is that you want to call the query method on each element of the profile_db list and not the list itself. Commented Jul 10, 2018 at 0:29

1 Answer 1

1

The specific reason for the AttributeError is you're assigning a map object to profile_db, then trying to use it as a sqlalchemy Session instance. A map object is only meant to iterate through the results of applying a function to an iterable, so naturally doesn't have a query method, which leads to the exception. To get the resulting Session class you'd have to iterate through the map object. You're also not instantiating a session anywhere in the example code.

The larger issue is I'm not sure why you're using map() at all. You seem to be trying to call sessionmaker once, with a single uri, so why not just call it and pass the uri? The sqlalchemy docs gives a basic example of using sessionmaker. Following that example, I believe you could do something like this:

# Create configured session class
Session = sessionmaker(bind=create_engine(CLIENT_URI), autoflush=True)

# Create new session and do query
session = Session()
some_profile = session.query(Profile).filter(Profile.id == req).first()
Sign up to request clarification or add additional context in comments.

3 Comments

This was a solution I had also tried earlier to amend this issue. The problem was, that in the line where you do: Session = sessionmaker(bind=create_engine(CLIENT_URI), autoflush=True) I was getting an AttributeError: AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'
Some digging into the sqlalchemy source shows that the _instantiate_plugins call occurs after attempting to parse the URL provided to create_engine. This suggests that there is a problem with your CLIENT_URI. Make sure that it's not None.
I'm not sure if this is the correct way to be going about it but I'm currently putting it in as CLIENT_URI = os.environ.get("CLIENT_URI")

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.