Using SQLAlchemy, determine which list values are not in a DB column
<p>Using SQLAlchemy, given a list, I'd like to determine which values in the list are not present in a given column in an sqlite DB table. One way to do it is the following:</p>

<pre><code>def get_user_ids_not_in_DB(self, user_ids):
    query__belongs = User_DB.user_id.in_(user_ids)
    select__user_ids_in_DB = self.SQL_Helper.db.query(User_DB.user_id).filter(query__belongs)
    user_ids_in_DB = zip(*select__user_ids_in_DB.all())[0]
    return list(set(user_ids) - set(user_ids_in_DB))
</code></pre>

<p>Is there a faster/more efficient way to accomplish the same thing?</p>

<p>Select all users then outer join it to an Aliased the User_db object   Then add a filter for non-Aliased user_id's that are null.</p>

<pre><code>   # an alias to a subquery on a  table. All user ids in you list
   ualias = aliased(User_DB, User_DB.user_id.in_(user_ids))

   results = self.SQL_Helper.db.query(User_DB.user_id)\
                 .outerjoin(ualias, ualias.user_id == User_DB.user_id)\
                 .filter(ualias.user_id == None)
</code></pre>

<p>Pardon typos, but that the gist of it.</p>

python python-2.7 sqlalchemy