0

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.

Shadow
  • 33,525
  • 10
  • 51
  • 64
cynthia
  • 26
  • 4
  • @Shadow - this is a valid question. You were too hasty. – coleifer Nov 04 '22 at 14:07
  • @coleifer I did not dispute that it was a valid question, just I think it has already been answered. You do not need a default value for the url column for it to be ignored in a duplicate column check. – Shadow Nov 04 '22 at 14:54

1 Answers1

1

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')
coleifer
  • 24,887
  • 6
  • 60
  • 75