Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging tables so I can add PK to an existing table #691

Closed
brunomunizaf opened this issue Jan 26, 2020 · 5 comments
Closed

Merging tables so I can add PK to an existing table #691

brunomunizaf opened this issue Jan 26, 2020 · 5 comments
Labels

Comments

@brunomunizaf
Copy link

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.

@brunomunizaf
Copy link
Author

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

@groue
Copy link
Owner

groue commented Jan 27, 2020

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 :-)

@brunomunizaf
Copy link
Author

@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.

@brunomunizaf
Copy link
Author

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

@brunomunizaf brunomunizaf reopened this Jan 28, 2020
@brunomunizaf
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants