1

I have a simple model I am inserting into. I am using the following syntax:

m = Model.objects.raw("insert into my_model (col) values ('test') returning *")

In postgres, this returns the identical columns that : select * from my_model where col = 'test' limit 1; would return. However, I am unable to get this to work. I had to resort to:

m Model(col='test')
m.save()
m = Model.objects.get(id = m.id)

I could also try the raw cursor syntax, but, it seems that the raw should work because the columns being returned map directly on to the model whose raw() I am using.

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
Greg
  • 6,571
  • 2
  • 27
  • 39
  • 1
    `m = Model.objects.create(col='test'); print(m.pk)` – Burhan Khalid Dec 08 '13 at 05:13
  • this works. but, i have triggers that do things to data when data is inserted. i want to get a copy of that 'new' data when the insert happens. for example, i have a trigger that inserts the timestamp of the insert and who did the insert. your idea works if everything is modeled in django, in my case, i do lots of data manipulation outside the scope of django. the main point is that insert ... returning returns a data set, if i did a select from table the objects.raw() would work. the raw() command seems to be outwitting itself. – Greg Jan 11 '14 at 19:02
  • I'm not sure exactly what you are trying to do, but once you save the model the reference will reflect the _saved_ instance; so if you have any db-side triggers they will be fired and the resulting object (row reference) will show the effects of those cursors/stored procedures. – Burhan Khalid Jan 11 '14 at 20:08
  • The point is I can run a select * from my_model where using the Model's raw() method. Since insert into my_model returning * returns the same data that select * returns, I would expect the Model's raw() method to perform the same for both. It doesn't. I think it is a bug. – Greg Jan 15 '14 at 23:31

0 Answers0