Django over Postgresql: using raw sql insert with returning statement not working

后端 未结 2 1257
灰色年华
灰色年华 2021-01-23 14:23

Using Postgresql 9.3 + PostGIS with django, I\'m trying to enter a record to my table and return the given id like so

def join(request):
    from django.db impor         


        
相关标签:
2条回答
  • 2021-01-23 15:09
    from django.db import connection, transaction
    
    def join(request):
        cursor = connection.cursor()
        id = cursor.execute("insert into table1 values(DEFAULT, 'temp', null,-4, null,null, null, null, DEFAULT, 1,null,DEFAULT,DEFAULT, null)")
        transaction.set_dirty()
        last_id = connection.insert_id()
        return HttpResponse(str(last_id))
    
    0 讨论(0)
  • 2021-01-23 15:14

    What is your database backend? If you use sqlite3,

    def join(request):
        from django.db import connection, transaction
        cursor = connection.cursor()
        cursor.execute("insert into table1 values(DEFAULT, 'temp', null,-4, null,null, null, null, DEFAULT, 1,null,DEFAULT,DEFAULT, null)")
        cursor.execute("SELECT last_insert_rowid() FROM table1")
        id = cursor.fetchone()[0]
        transaction.set_dirty()
    
        return HttpResponse(id)
    
    0 讨论(0)
提交回复
热议问题