Search code examples
ormpeewee

Peewee how can I IGNORE columns using get_or_create? Duplicates


I have four columns id, name, industry, url

I am asking whether there is parameter that ignores a column in get_or_create. Not a unique together constraint. Since url can change, I want peewee to ignore the column url when adding using get_or_create.

I want it to check whether name and industry are already present in the table. If they are, then it does not add a duplicate entry. If they are not, then it adds a new entry.


Solution

  • Sure you would want to, in that case, use the defaults parameter:

    Model.get_or_create(
        name='the name',
        industry='the industry',
        defaults={'url': 'https://...'})
    

    The above would first attempt to fetch a match on name and industry. If no match is found, then one is created with the given URL. The above is equivalent to:

    try:
        m = Model.get(name='the name', industry='the industry')
    except Model.DoesNotExist:
        m = Model.create(
            name='the name',
            industry='the industry',
            url='the url')