lang2
lang2

Reputation: 11966

alembic: adding id field to existing table

This is a follow on to my previous question. Basically I have a table that has 2 foreign key links into two other table. But I think the best way is to add a primary key to the table itself so that it becomes:

id, int, primary
foreign_id_1, int, primary
foreign_id_2, int, primary

The problem is that there are already a lot of items in the table. So when I ran alembic upgrade head, it add 0 as the value of all the existing items. This apparently breaks my code.

Is there a way to retrospectively add ids to the existing items?

Upvotes: 1

Views: 774

Answers (2)

univerio
univerio

Reputation: 20508

What you want is not an additional id column as the primary key. What you want instead is to keep your existing composite primary key and add a surrogate id column (not a primary key column) that auto-increments to keeps track of the order of insertion. But because you've already inserted all your rows into the table, you've lost the order, so it makes no sense to "recover" that order. You can simply assign 0 to that column without issue.

Upvotes: 1

Danil
Danil

Reputation: 5171

You shouldn't have a multiple primary keys in one table. See this question. What you want to do? Perhaps, your problem may be solved with two column index

Upvotes: 1

Related Questions