Background:
The application I am currently developing is in transition from SQLite3 to PostgreSQL. All the data has been successfully migrated, using the .dump
from the current database, changing all the tables of the type
CREATE TABLE foo (
id INTEGER NOT NULL,
bar INTEGER,
...
PRIMARY KEY (id),
FOREIGN KEY(bar) REFERENCES foobar (id),
...
);
to
CREATE TABLE foo (
id SERIAL NOT NULL,
bar INTEGER,
...
PRIMARY KEY (id),
FOREIGN KEY(bar) REFERENCES foobar (id) DEFERRABLE,
...
);
and SET CONSTRAINTS ALL DEFERRED;
.
Since I am using SQLAlchemy I was expecting things to work smoothly from then on, after of course changing the engine
. But the problem seems to be with the autoincrement of the primary key to a unique value on INSERT
.
The table, say foo
, I am currently having trouble with has 7500+ rows but the sequence foo_id_seq
's current value
is set on 5
(because I have tried the inserts five times now all of which have failed).
Question:
So now my question is that without explicitly supplying the id
, in the INSERT
statement, how can I make Postgres automatically assign a unique value to the id
field if foo
? Or more specifically, have the sequence return a unique value for it?
Sugar:
Achieve all that through the SQLAlchemy interface.
Environment details:
- Python 2.6
- SQLAlchemy 8.2
- PostgreSQL 9.2
- psycopg2 - 2.5.1 (dt dec pq3 ext)
PS: If anybody finds a more appropriate title for this question please edit it.