Grdb.swift: Merging tables so I can add PK to an existing table

Created on 26 Jan 2020  路  5Comments  路  Source: groue/GRDB.swift

Hi,

I'm looking to add primary key to an existing table that doesn't have it and I haven't been able to just alter the current table with .autoIncrementedPrimaryKey so I figured out I'd have to create a temp copied table with .autoIncrementedPrimaryKey and then delete the old table and rename the copied table to the previous one's name - or I could just merge two tables but I couldn't find any merge reference on GRDB docs. Is there a merge functionality or I should do it through SQL statement?

PS: If there is an easier way to do this please let me know - i'm a complete ignorant in DB management.

Thank you,
Bruno.

support

Most helpful comment

Hello @brunomunizaf,

I suggest you just apply the recipe you described (create a new table, dump the contents into it, drop old table and rename the new one), and that's it.

Do not submit any pull request, unless you want to become much less ignorant in DB management :-)

All 5 comments

According to #575 it seems that I'd have to implement it myself to contribute.

Hello @brunomunizaf,

I suggest you just apply the recipe you described (create a new table, dump the contents into it, drop old table and rename the new one), and that's it.

Do not submit any pull request, unless you want to become much less ignorant in DB management :-)

@groue my solution went pretty bad. I couldn't just select * from the old table and insert into the new one cause it said it had x columns and only provided x-1 values but I can't predict the autoincremented pk. So i decided to try another approach with the hidden rowid PK but for that I gotta ask you how to get this value since I don't have it set as a property from the PersistableRecord model. Could you give me a tip here? Thank you.

I also wonder why executing "ALTER TABLE table ADD PRIMARY KEY id" doesn't work. 馃

I guess this solves it: https://github.com/groue/GRDB.swift#advanced-database-schema-changes

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dev-dubaisoftwaresolutions picture dev-dubaisoftwaresolutions  路  4Comments

imabuddha picture imabuddha  路  4Comments

cody1024d picture cody1024d  路  7Comments

Mina-R-Meshriky picture Mina-R-Meshriky  路  5Comments

mtissington picture mtissington  路  6Comments