In simplified form, I have two models:
class Chart(models.Model):
barcode = models.CharField()
class Visit(models.Model):
chart = models.ManyToManyField('Chart')
I would like to add a new chart to an existing visit.
I have the correct visit object and I created a new chart object
visit_obj = Visit.objects.get(pk=visit_pk)
print('visit id', visit_obj.id) #this is correct
new_chart = Chart()
new_chart.barcode = 'something'
new_chart.save()
print('new_chart id', new_chart.id)
I add the chart to the visit:
visit_obj.chart.add(new_chart)
Two things happen:
-
The primary key on the new_chart object is not new. It is an ID that has been created and now there are two objects with that ID.
-
The crosswalk table between the two models shows that a new record got inserted with the chart_id that belongs to the new chart (and also an old chart), but the visit_id is completely wrong. It doesn’t match the visit_id from my visit_obj.
I am using Snowflake as my database.
I am completely baffled and would appreciate help.
Thank you