pymssql INSERT won't auto increment ID

时间:2017-10-12 09:43:32

标签: python sql sql-server django pymssql

I am using pymssql in my django project to connect to a database. I'm trying to use execute() method to do an insert.
However I get this error:

Cannot insert the value NULL into column 'ID', table ITEMS

the ID column is primary key and so it should be filled by the sql itself as I understand. here is my insert command:

    bill_id = execute("""
       INSERT INTO ITEMS(COlUMN1,COlUMN2, COlUMN3,COlUMN4,COlUMN5)
       VALUES (%d, %d, %d, %d, %d);
        """, (1713, 6, 929, 1184, 25))

and none of these COLUMNs are ID. can anyone tell me what am doing wrong?

1 个答案:

答案 0 :(得分:3)

If id column is not an auto increment key column, it will not work. If you define a column in SQL within a create table statement it is not automatically an autoincrement column. You must define it. Perhaps you should make at first a query to get the max value of id column. At second you can increment the value und add the primary column to your insert statement.

I found this link to use django meta data api.

https://docs.djangoproject.com/en/1.9/ref/models/meta/#migrating-from-the-old-api