Adding IntColumn as primary key to existing table #2062
-
Hi there, I am currently trying to add an IntColumn to an already existing table.
To this one:
Using the IntColumn as primaryKey would probably be optional, but it should not be nullable.
But, since rowId can be null, this throws an error. Is there a way to just iterate through the column and write an index into the field? Thanks in advance, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Your approach for the migration gets pretty close. For me, it works after I remove the Technically speaking, the problem is not that (I really like the |
Beta Was this translation helpful? Give feedback.
Your approach for the migration gets pretty close. For me, it works after I remove the
addColumn
statement altogether - the singlealterTable
call is enough.Technically speaking, the problem is not that
rowId
can be null (it can't). Your migration fails at theaddColumn
call since the added column is non-nullable. Non-nullable columns can't be added viaALTER TABLE ADD COLUMN
(whichaddColumn
uses internally) because existing rows in the table would illegally have a null value then.On the other hand,
alterTable
runs a rather complex procedure that involves creating a new table from scratch, copying data over, and then replacing the old table with the new one. This means that it can crea…