Hi, I am having an issue switching to prisma. I go...
# orm-help
p
Hi, I am having an issue switching to prisma. I got a postgres db. We've been using it with sequelize but I am trying to switch to prisma 2. All our tables are camelCase and plurals. e.g.
companies
or
userProfiles
table. When I run
prisma introspect
it generates models that matches the table names, which results in
nexus
generating "singular" and plural typings for the models, both being the actual table and model names (plural) so it results in duplicates in the typescript typings which means code doesn't compile. I got over 3000 lines generated by prisma introspect and there's no proper intellisense to rename a model and all fields related to it
j
probably more targeted in #graphql-nexus
p
Thanks @janpio It's a bit all over the place though. The issue is when using nexus with prisma, but the problem is more when prisma generates the models wrong
j
Why do you think that Prisma generates the models wrong?
p
Because Prisma docs says that it does 🙂
🐳 1
let me find the link quick
👍 1
j
That is formulated a bit... strange.
p
IDD
j
Prisma has no way to reliably go from
users
to
User
- especially not in other languages. Hence we decided to not even try do to that.
p
ok so what's the solution here?
j
To what problem exactly?
p
I spent about 40min trying to rename the fields manually but the further I go down the rabbit hole the worse it gets
j
Are you using our VSCode extension?
p
yes I am
j
It has a nice functionality on
F2
that at least updates the references and relations as well at the same time.
p
but it doesnt have intellisense or a way to rename a model. it just has colors for the items
I tried that F2 rename like I am used to in JS/TS but it didnt do anything
j
What version of the extension are you using?
p
I installed latest today
2.4.0
j
Hm, try upgrading to 2.4.1. But this is what it should look like on F2:
p
I checked marketplace now and see there are 2 other extensions available too. Am I even using the right one?
j
If you are using "Prisma", then you are using the correct one.
p
Did you just release 2.4.1 now? When I got the version number above a few seconds ago, it showed as latest. I reloaded vscode just to see if that fixes it and now it says 2.4.1 is out
Thanks intellisense is now there with 2.4.1
j
We released that a bit ago today, few hours.
(VSCode marketplace is sometime really a black box)
There might be some community tools to automate the renaming as well, but I am not sure if one covers exactly that use case.
p
I tried googling around but could not find a codemod or anything
didnt know what other buzzword to use
j
same problem for me now 😉
(ping @Ahmed - maybe you know?)
p
I tried searching for "sequelize to prisma migration" but that just had a bunch of "prisma vs sequelize vs typeorm" articles
j
But your request gave me a feature idea for the VSCode extension: https://github.com/prisma/language-tools/issues/391
p
+1 to that idea
j
Buuuuut, more general: The model naming is a convention. If you are fine with the table names, you can keep those as well.
p
ideally
prisma introspect
should be able to either guess whether all table names are plural/singular or if it cant guess it should ask in the cli before generating the schema. Most of the time teams will have a convention and use either singular or plural and not both
The model naming is a convention. If you are fine with the table names, you can keep those as well.
You say that, but then when you put nexus into the mix, you end up with types duplicated and typescript doesnt like that.
j
Problem is that going from plural to singular is not a solved problem for all languages. In English it also only works in most cases. And many databases are inconsistent in their usage, so it is really hard to generalize.
Your actual problem seems that Nexus then clashes in some way, which it probably just should not do. I hope someone will reply to that in the channel.
Nexus made that decision (and thus creates this problem) independent of Prisma and how Prisma handles model names.
p
nexus-plugin-prisma/schema
will go and look at your prisma schema and create types. For some stupid reason it wants to compile a new name for when its a plural, despite prisma2 using .findOne and findMany convention as opposed to prisma1 using singular/plural
Problem is that going from plural to singular is not a solved problem for all languages. In English it also only works in most cases. And many databases are inconsistent in their usage, so it is really hard to generalize.
I agree. But if its so hard, why try to go the other direction?
Nexus made that decision (and thus creates this problem) independent of Prisma and how Prisma handles model names.
Yeah I guess it's a bit of a bigger issue now. From a user's perspective nexus=prisma but I know there's different maintainers
j
Nexus just did not catch up really - and your problem shows that it should 😉
p
Problem is nexus isnt well maintained at the moment. There are open issues related to this for the past few months and nothing has been done
j
So I agree with
But if its so hard, why try to go the other direction?
p
Yep
So what's the interim solution? should I not use nexus and use a SDL solution?
j
A lot has been done, but we agree that we need to refocus. Actually had a lot of meeting about that this week, so expect movement in the next few weeks here.
Puh, maybe someone from the Nexus team knows a workaround.
Or you rename all the models once manually.
a
@Pieter your issue with
nexus-plugin-prisma
p
yeah but next time the DB changes, I need to run
introspect
again and then do it all over again.
j
Or you can use any SDL first solution of course.
Fortunately not. Let me get you a link.
You can use this experimental feature (that works pretty well) to keep any manual renames you did: https://github.com/prisma/prisma/issues/2829
npx prisma introspect --experimental-reintrospection
a
so you can use nexus without prisma plugin maybe my plugin not have issues with plural
j
(If you go with SDL first, please make that heard in an Nexus issue that the plural thing made you do that.)
a
@janpio it’s not nexus issue it’s prisma plugin
p
@janpio I spoke too soon. 2.4.1 does NOT let me rename. It makes it seem like I can, but then I get a
no result
notification from vscode
j
@Ahmed The GH org is
graphql-nexus
, that is what I refer to here. Or the Slack channel #graphql-nexus. Where the issue lives is rather unimportant - it is a Nexus problem.
1
👍 2
p
npx prisma introspect --experimental-reintrospection
That seems useful
prisma rainbow 1
j
@Pieter You might need to do that in the
model
definition, not where it is used as a type. (Trying that now myself and creating an issue)
p
so you can use nexus without prisma plugin maybe my plugin not have issues with plural
What is the side effect? no generated types for prisma context but still typings for the gql schema?
(If you go with SDL first, please make that heard in an Nexus issue that the plural thing made you do that.)
Will do
👍 1
@janpio you are right. using it in the model definition line itself works. This is super strange behaviour and not what any vscode user is used to
j
(Best just comment your last sentence on that as well)
Same "No result" problem on all strings beside the one behind
model
it seems - https://github.com/prisma/language-tools/issues/395 - so we have some room to improve there. Pretty new everything 😄
a
@Pieter you can use nexus generator or SDL first generator with my cli https://paljs.com/ because i using findOneX findManyX so no conflict
and if you have an issue we can get solution
p
sorry for going quiet for 20min. I was busy renaming my 127 models
fast parrot 1
@janpio I left a comment on the issue. thanks for opening it
prisma rainbow 1
@Ahmed checking paljs
💯 1
j
Let us know how Prisma works in general with 127 models 😄 Introspection resulsts were fine otherwise?
👀 1
p
nope, the same problem is on the field names
a
@Pieter you mean using paljs?
p
For example: I'd have a
clients
table which has a 1:1 relationship with
companies
table, which has a column of
owner
which is a foreign key of the
id
column on
clients
prisma.schema would render
Copy code
model companies {
  id        Int   @default(autoincrement()) @id
  ownerId   Int
  clients   clients @relation(fields: [ownerId], references: [id])
}
@Pieter you mean using paljs?
nope was a reply to @janpio about how introspection works otherwise
ownerId
should probably have a directive making it a foreign key right?
clients
should be
client
but due to the model name being
clients
from the table name, the field is also
clients
even though it is singular.
If it should really be a plural it needs the
[]
array syntax
j
The (default, introspected) name stupidly comes from the model/table name 😕
🤨 1
💯 1
The FK definition lives on the "relation fields" (in your case
clients
) in Prisma schema world.
But yeah, not optimal yet - really hard to solve universally.
a
@janpio you can’t do anything for this case
j
We have the concept of "Introspection Configuration" that we want to tackle after Re-Introspection that should allow users like you to set their own preferences. But even then the signular/plural thing is hard to get rid and will probably be some kind of wizard.
p
Check this rename out. It adds a
@@map
when I use rename, but it adds it in the wrong place (probably because the one field EK is plural) which breaks everything https://cloud.invisible.email/5zuGD70Q
j
lol
ok, not funny for you
Can you create an issue in that repo?
p
We have the concept of "Introspection Configuration" that we want to tackle after Re-Introspection that should allow users like you to set their own preferences.
But even then the signular/plural thing is hard to get rid and will probably be some kind of wizard.
+1 for some config. I agree its tough to solve it universally. You need some user input to really know which problems there are to solve. Config will help. Plural issues in general are hard, even for eng only. A wizard will be nice. Guess the correct model and ask the user in the cli wizard to hit enter if its correct or to fill in the correct name
@janpio sure let me create a issue
👍 1
j
Yep, exactly something like that. Or possibly much better tooling in the VSCode extension. Think a list of just model or relation names to bulk edit these, and then they are applied to the schema.
fast parrot 1
❤️ 1
p
j
Ha, that gave me an idea how to implement that and I put it into an issue as well: https://github.com/prisma/language-tools/issues/391#issuecomment-669498196
prisma rainbow 1
Thanks for the ping pong here - that was very fruitful. Hope we can work on some of these soon.
p
Thanks for all the help. I'll keep the pingpongs coming 🙂
Copy code
yarn run v1.22.4
$ prisma generate && ts-node --transpile-only scripts/generateApolloSchemas.ts
Environment variables loaded from prisma/.env
Error: 
Error: The schema at "/prisma/schema.prisma" contains reserved keywords.
       Rename the following items:
         - "model AggregateBalance"
To learn more about how to rename models, check out <https://pris.ly/d/naming-models>


error Command failed with exit code 1.
That's the weirdest reserved keyword I've seen in my life
j
Ha, probably just
Aggregate
. Also worth an issue, to double check if that makes sense even as part of model names.
(It is not immediately obvious to me why - so maybe just a too relaxed matcher)
p
@janpio posted #398
j
p
oh I assumed reserved words are part of language tools. my bad
j
No problem at all - they are actually all over the stack.
p
🙂