ilyakatz / data-migrate

Migrate and update data alongside your database structure.
MIT License
1.4k stars 192 forks source link

Data migration object create methods not recognizing the proper primary key automatically. Primary key must be specified. #293

Open tglover-primexinc opened 10 months ago

tglover-primexinc commented 10 months ago

When creating an object in a data migration, it attempts to set the primary key ID to be 1, instead of the next available key in the sequence. Simultaneous runs will version it up from 1 to 2, etc. but it should be checking the existing database where there are 40 records, and set the primary key to 41.

StandardError: An error has occurred, this and all later migrations canceled:
PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "chip_architectures_pkey"
DETAIL:  Key (id)=(1) already exists.

Is there something I need to do in configuration to get the migrations to recognize the common primary key authority like it would in regular migration files?

I'm having to add a hack to manually version up the id when creating an object: id: Model.maximum(:id).to_i.next

It seems that I shouldn't need to do this and it should be assigned automatically.

tglover-primexinc commented 10 months ago

Apologies. This was due to a special circumstance and not related to the gem. I was seeding with static primary and foreign keys, and that created the sequence table to become out of sync.