Search code examples
pythonpostgresqlweb2py

Updating Web2py's auto_increment id with filled database?


i'm working on a web2py project and i need to insert 500+ rows of data to a database.

The problem is that if i use Pgadmin to do this using 'Insert' web2py auto_increment id, it doesn't recognizes the data and tries to insert new rows using the KEY 1 which leads to an REPEATED PRIMARY KEY ERROR.

Is there a better way to insert the data initially or modifying the starting auto_increment id value?

UPDATE:

I've defined and created the table on Web2py like this,

db.define_table('Client_Size',
            Field('name', 'string'))

After running it one time web2py created it on the db, and then, using Pgadmin III, interted values like this,

INSERT INTO Client_Size(id, name) VALUES
(1, 'Small'), (2, 'Medium'), (3, 'Big');

This is a short input, but i have another with 500 values so manual input is not a possibility.

After that, if i try to add a new row to Client_Size web2py doesn't recognize that the db has been filled with data, and uses the id as 1, creating the error.


Solution

  • The id field is an auto-incrementing integer field, which means the database will automatically fill it in with the next value. So, you should not be attempting to insert your own values for that field -- just specify the value of the name field and let the database handle creation of the id. (Even if it were not auto-incrementing, because it serves as the primary key for the table, you wouldn't want to insert duplicate values anyway.)