Hey! How would you save the connection/relation be...
# orm-help
m
Hey! How would you save the connection/relation between the old & new model when changing a model's name? I know it's not possible with Prisma to do that kind of things but when I would implement that on my own, how could I do that? I have an idea to store the table names in a migration-like database table, any thoughts on this? Would be cool to hear some opinions/feedback. 🙂
r
Hey @Marvin 👋 I think this should be possible with the early access version of Migrate which is supports altering changes via SQL. https://prisma.slack.com/archives/C01739JGFCM/p1604078848016200 You could try this one and check 🙂
💯 1
m
@Ryan Thanks Ryan, didn't know I could solve that with the new migrate too. Really great to hear that! 🙂 So that I understand it correctly: I would run the migration, change the SQL with a custom script and then run it again?
r
Yeah, so something like: 1.
prisma migrate --draft
2. Change the SQL accordingly 3.
prisma migrate up
m
Great, I will try that. Thanks again. :)
💯 1
I've tested it a bit and can't find any good solution to get my connection between old and new model/table name. I see the draft creates a
CREATE TABLE
with the new name and a
DROP TABLE
with the old name. But how can I know if they belong to the same model? With only one change at a time, it's more or less clear because I can just search for the
CREATE TABLE
and
DROP TABLE
statements where I then can extract the names from and use them in a
.ts
file/script which manipulates the migration SQL draft file. With multiple changes it's kinda tricky when there are more than one
CREATE TABLE
&
DROP TABLE
statements. Any idea how I could solve this? Is there maybe something I can use from the SDK or so? As an example: I create the schema dynamically with
.ts
files where I can provide a name in the function call (passed as argument). This function creates the model schema. Now when I change the name later, it should automatically migrate the data from the old table to the new one. So it needs to know the old name on execution which is obviously gone. :(
r
Possibly @Joël might have more info here. Currently we do not support
.ts
files though, only
.sql
.
m
I meant that I do that with "own" TS files, Prisma shouldn't handle that. The possibilities you have with SQL are way better but I still struggle with the renaming stuff beside the typical Prisma migrate. It's just not 100% clear, how I know which table name was changed to the new name, so I can do my own adjustments (for example remove the CREATE and DROP statements from the SQL and add ALTER TABLE). This all happens programmatically via functions, that's why I need the exact name before and after that. I hope you understand what I mean, a bit hard to explain 🙂
r
So as an example, you would like to rename a table. Prisma currently drops the entire table and re-creates it, and you would like to change that right?
m
Yes, right. Currently, I search for the
CREATE TABLE
&
DROP TABLE
statements in the generated SQL draft, extract the names from it and then create a
ALTER TABLE
statement from it which will be then inserted into the SQL draft. Later I can run the migrate command and it will be updated. That works good if you only have one
CREATE TABLE
and one
DROP TABLE
statement, but I think you can't be 100% sure these both statements are connected. Especially if you have more than one from each, you can't say which
CREATE TABLE
is connected to which
DROP TABLE
statement.
a
Hello @Marvin, the approach you lay out here sounds like the best for now. We do have plans to make it easier to handle renames in the future, but it will still take some time until we can work on that. With the approach we have in mind you would not have to manually find the relevant SQL statements and update the migration script.
💯 1
If you are not yet, you should join #product-feedback so you can find out when we add that capability 🙂
m
@Alberto Perdomo Hey Alberto, thanks for your feedback and the confirmation! It's totally fine for me now, just was an idea I had. Really love to experiment with Prisma and see what's possible. 🙂 I also already joined the channel and I'm hyped every time there is a new message. 😄 Keep the superb work up! prisma rainbow
🚀 1