﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
13915	getting and creating with get_or_create behave differently when using a foreign key	KyleMac	nobody	"Using the following models:
{{{
class Product(models.Model):
    name = models.CharField()

class Stat(models.Model):
    product = models.ForeignKey(Product)
    value = models.PositiveIntegerField()
}}}

If you do the following and the row doesn't exist:
{{{
Stat.objects.get_or_create(product__id=1, defaults={'value': 0})
}}}
you will get an exception that product_id cannot be null (i.e. the create is failing).

However, if you do the following instead:
{{{
Stat.objects.get_or_create(product_id=1, defaults={'value': 0})
}}}
you get an exception that product_id cannot be resolved to a field (i.e. the get is failing).

The solution is to do something like:
{{{
Stat.objects.get_or_create(product=Product(id=1), defaults={'value': 0})
}}}"		closed	Database layer (models, ORM)	1.2		duplicate			Unreviewed	0	0	0	0	0	0
