Standard way of Ensuring something exists before creating it?
0

4 posts in this topic

I want to create a project (or Sequence, or Shot, or AssetVersion, etc....) but right now i'm using all kinds of different ways of checking to see if something exists before i create it.  I'm guessing there's some simple way of doing this that i'm just not aware of.   For instance.  To Create a Project Currently I'm doing something like this:

 

project_data = session.query('Project where status is active and name is %s' % 'cgl_unittest2').first()
print project_data
if not project_data:
    project_data = session.create('Project', {
        'name': 'cgl_unittest2',
        'full_name': 'cgl_unittest2',
        'project_schema': project_schema
    })

 

Problem is when unsuccessful these queries return:

None, QueryResult, or sometimes they return an error.   So i'm creating all kinds of workarounds for the fact that there is often different results returned depending on the type i'm querying.  

is there some kind of 'safe' flag on the create() function or some other way of checking to see if something exists before creating it?  

Share this post


Link to post
Share on other sites

Ok I'm learning more about this already.  

QueryResult happens when i fail to put .one(), first(), or all() on the end of my query.   This is going to make a huge difference moving forward.  Going through and fixing some of these right now and tightening up my code.  

Still curious as to if there's another way other than querying and then creating based off the results.   But this is getting cleaner!

Share this post


Link to post
Share on other sites

So here's some more information just if anybody is interested:

This code is searching for a sequence that doesn't exist:

seqs = session.query('Sequence where name is "{0}" and project.id is "{1}"'.format('060', project_data['id'])).first()

.first() returns None

seqs = session.query('Sequence where name is "{0}" and project.id is "{1}"'.format('060', project_data['id'])).one()

.one() returns:

Traceback (most recent call last):
  File "C:/Users/tmiko/PycharmProjects/cglumberjack/src/plugins/project_management/ftrack/ftrack_unit_tests/ftrack_tests.py", line 16, in <module>
    seqs = session.query('Sequence where name is "{0}" and project.id is "{1}"'.format('060', project_data['id'])).one()
  File "C:\Users\tmiko\AppData\Roaming\Python\Python27\site-packages\ftrack_api\query.py", line 167, in one
    raise ftrack_api.exception.NoResultFoundError()
ftrack_api.exception.NoResultFoundError: Expected result, but no result was found.

This is the one that is screwing me up - so i'll avoid using this

.all() returns:

[]

So that's a good option as well.  

If i don't have anything telling ftrack what to return i get a QueryResult object

 

This at least demystifies why i'm getting different results.

Share this post


Link to post
Share on other sites

Hi @Tom Mikota, glad to see you managed to get your head around the query system ! 

overall to keep in mind:

  • <query>.all() --> return all result or an empty list
  • <query>.one() --> return the result or error if not found
  • <query>.first() --> return the result or None if not found

You can find more details in our api documetation

Hope it helps.
L.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
0