I am using bulk_create on a ManyToMany relationship and I think there is a bug. Here is the code: class Company(models.Model): url = models.URLField(unique=True)
class Website(models.Model): url = models.URLField(unique=True) companies = models.ManyToManyField(Company, null=True, blank=True)
list_companies = [Company(url='...'), Company(url='...')] w.companies.bulk_create(list_companies) -> [< Company: Company object>, < Company: Company object>] #The 2 Company objects are created
w.companies.all() -> [] # But the relationship is broken
The 2 companies are created but I cannot access to them through my many-to-many relationship. I looked at the database and yes I can find the 2 companies in the company table but no rows in the website_company table.
What do you think ? Do you have the same behavior ? Lemme know if I do it wrong.
> I am using bulk_create on a ManyToMany relationship and I think there is a
> bug.
> Here is the code:
> class Company(models.Model):
> url = models.URLField(unique=True)
> list_companies = [Company(url='...'), Company(url='...')]
> w.companies.bulk_create(list_companies)
> -> [< Company: Company object>, < Company: Company object>] #The 2
> Company objects are created
> w.companies.all()
> -> [] # But the relationship is broken
> The 2 companies are created but I cannot access to them through my
> many-to-many relationship.
> I looked at the database and yes I can find the 2 companies in the company
> table but no rows in the website_company table.
> What do you think ? Do you have the same behavior ?
> Lemme know if I do it wrong.
Hmmh, I think you have found an oversight. Please open a ticket in
Trac. It is possible the fix is going to be just to disallow use of
bulk_create through relations, as fixing this properly is going to be
hard (to create the relation you need the PK of the objects created,
and bulk_create doesn't provide the PK if it comes from a
sequence...). Or, maybe just document that no, the relations are not
created for you. For reverse foreign key this could be fixed
however...
> On May 7, 10:53 pm, Michael <michael.palumb...@gmail.com> wrote: > > Hi,
> > I am using bulk_create on a ManyToMany relationship and I think there is > a > > bug. > > Here is the code: > > class Company(models.Model): > > url = models.URLField(unique=True)
> > list_companies = [Company(url='...'), Company(url='...')] > > w.companies.bulk_create(list_companies) > > -> [< Company: Company object>, < Company: Company object>] #The 2 > > Company objects are created
> > w.companies.all() > > -> [] # But the relationship is broken
> > The 2 companies are created but I cannot access to them through my > > many-to-many relationship. > > I looked at the database and yes I can find the 2 companies in the > company > > table but no rows in the website_company table.
> > What do you think ? Do you have the same behavior ? > > Lemme know if I do it wrong.
> Hmmh, I think you have found an oversight. Please open a ticket in > Trac. It is possible the fix is going to be just to disallow use of > bulk_create through relations, as fixing this properly is going to be > hard (to create the relation you need the PK of the objects created, > and bulk_create doesn't provide the PK if it comes from a > sequence...). Or, maybe just document that no, the relations are not > created for you. For reverse foreign key this could be fixed > however...
> Le lundi 7 mai 2012 22:04:31 UTC+2, Anssi Kääriäinen a écrit :
>> On May 7, 10:53 pm, Michael <michael.palumb...@gmail.com> wrote: >> > Hi,
>> > I am using bulk_create on a ManyToMany relationship and I think there >> is a >> > bug. >> > Here is the code: >> > class Company(models.Model): >> > url = models.URLField(unique=True)
>> > list_companies = [Company(url='...'), Company(url='...')] >> > w.companies.bulk_create(list_companies) >> > -> [< Company: Company object>, < Company: Company object>] #The 2 >> > Company objects are created
>> > w.companies.all() >> > -> [] # But the relationship is broken
>> > The 2 companies are created but I cannot access to them through my >> > many-to-many relationship. >> > I looked at the database and yes I can find the 2 companies in the >> company >> > table but no rows in the website_company table.
>> > What do you think ? Do you have the same behavior ? >> > Lemme know if I do it wrong.
>> Hmmh, I think you have found an oversight. Please open a ticket in >> Trac. It is possible the fix is going to be just to disallow use of >> bulk_create through relations, as fixing this properly is going to be >> hard (to create the relation you need the PK of the objects created, >> and bulk_create doesn't provide the PK if it comes from a >> sequence...). Or, maybe just document that no, the relations are not >> created for you. For reverse foreign key this could be fixed >> however...