cakephp / cakephp/phinx

Unable to add an AUTO_INCREMENTING primary key to a populated table that doesn't have one

Open
#1,880 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

support
Dominant language
PHP
Stars
4.5k
Forks
884
PR merge metrics
No merged PRs in 30d

Description

$tag_data = $this->table('tag_data');
$tag_data
->addColumn(
	'td_id',
	'integer',
	[
		'null' => false,
		'signed' => false,
		'after' => 'tag_id'
	]
)
->changePrimaryKey('td_id')
->save();

This doesn't work because it is creating the td_id column and initializing all rows to 0.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the addColumn and changePrimaryKey calls in the migration API and how they handle an existing populated table. Reproduce the shown migration, then inspect related database migration tests if available; done means adding an auto-incrementing primary key without initializing existing rows to zero.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.