Basic question about relations

Apologies if this is a super basic question. I am coming to Redwood from a PHP, MySQL, Laravel background.

Working through the tutorial I thought I would add an author to the blog post.

My ‘schema.prisma’ is like this:

model Post {
      id        Int      @id @default(autoincrement())
      title     String
      body      String
      author    User     @relation(fields: [authorId], references: [id])
      authorId  Int
      createdAt DateTime @default(now())
    }

    model User {
      id    Int    @id @default(autoincrement())
      name  String
      posts Post[]
    }

I used the generator to create all the scaffolding for the CRUD operations. Everything worked fine until I tried to add a blog post. I had already added a user with an id of 1.

To get it to work I had to change my ‘services/posts/post.js’ file from this:

export const createPost = ({ input }) => {
  return db.post.create({
    data: input,
  })
} 

To this,

export const createPost = ({ input }) => {
  return db.post.create({
    data: {
        title: input.title,
        body: input.body,
        author: {
          connect: {
            id: input.authorId
          }
        }
      }
  })
}

Firstly, is this even correct? If so is it necessary to manually code this type of solution, i.e. when you add relations does this mean the generators cannot automatically wire everything up?

Thanks!

Hi @chris.johnson Quick reply for now – it can be a little confusing at first to understand the db is the Prisma Client object from api/src/lib/db.js. So these are the docs you’ll need to reference:

Sounds like you did get it working, correct? Are you just hoping to confirm this is the “right way” or are you attempting to figure something else out as well.

Thanks for diving into Redwood with us!

Hi @thedavid. Thanks for your reply. Yes, it is working. I am, as you say, hoping to confirm if this is the right way. I will have a good read through the docs you referenced.

Maybe it already exists and I just haven’t found it, but it would be super useful if there was a Redwood example (like the todo app) which used all the common relations (i.e. one-to-one, one-to-many and many-to-many) and some associated queries (i.e. create including one-to-one condition, create including one-to-many condition, etc…).

As you say, it can be a little confusing trying to understand the context of the db and the Prisma client so some detailed Redwood examples would be useful.

It would also be good to know the limitations of the generators, i.e. I setup a valid ‘schema.prisma’ and used the CRUD scaffolding generator but the output wasn’t something that actually worked. I am not really clear if this was supposed to work, or the generator cant setup the scaffolding correctly if you use relations?

Thanks for your time.

1 Like

Sorry to drop off the radar here! This feedback is on point with other conversations that seem to be picking up momentum, and it’s definitely of high value to me.

Have you seen this parallel thread with similar discussion? I just added a comment (see bottom of thread) with a proposal for scheduling a virtual kind of “office hours” to explore how we, as a community, can improve the Redwood API learning curve --> Groking redwood

It would also be good to know the limitations of the generators, i.e. I setup a valid ‘schema.prisma’ and used the CRUD scaffolding generator but the output wasn’t something that actually worked.

^^ This definitely is not clear and needs to be. I just created this Issue for a new doc: https://github.com/redwoodjs/redwoodjs.com/issues/211

And, if you haven’t seen it already, I’m curious if this post is what would have been helpful to know upfront --> Prisma Beta.2 and RWJS: Limited Generator Support for Relations (with workarounds)

No worries, I appreciate the great support you guys are providing in the community.

I have been making my way through the Prisma docs and it does seem to offer a really great solution to managing and working with a database. As you say though, the Redwood API isn’t necessarily that easy to grok without delving deep into the docs so some of the potential can be missed. i.e. functions like the nested writes look super useful.

Coming at Redwood from a PHP, MySQL, Laravel background I was drawn to Redwood by the notion of it being truly full stack, as oppose to something like Next.js where it is still necessary to have a ‘backend’ somewhere else. Lots of frameworks can do the frontend stuff but to have the concept of services and a backend which is connected is an amazing concept.

I know it is easier said than done but some video tutorials and cookbooks focused on services would be super useful in terms of showcasing their true potential and to illustrate to a casual observer how they can truly make your Redwood app full stack. Undoubtedly, in my opinion, sites like Laracasts have really helped grow the Laravel community. I know it’s early days, but something akin to that could be amazing for the adoption curve of Redwood.

On another topic I have just read in the newsletter, which was great btw, that testing has had some focus. As someone who likes to TDD this is another area that I feel would help Redwood adoption.

The informal meetup sounds good, I am in France so timing might be abit awkward but I will try and join (kids and wife permitting!)

1 Like

I know it is easier said than done but some video tutorials and cookbooks focused on services would be super useful in terms of showcasing their true potential and to illustrate to a casual observer how they can truly make your Redwood app full stack. Undoubtedly, in my opinion, sites like Laracasts have really helped grow the Laravel community. I know it’s early days, but something akin to that could be amazing for the adoption curve of Redwood.

^^ :100: and the momentum here is definitely growing. Looks like Dom is attempting a re-write of LearnGraphQL using Redwood as the focus. And I think there’s some good momentum in the Groking Redwood thread as well. So please keep upvoting and adding comments to Issues and discussions where you see them.

As someone who likes to TDD this is another area that I feel would help Redwood adoption.

^^ YES! Testing has been getting a major boost the last month thanks to Robert. What’s missing now is the corresponding docs and updating the test templates from Generators. But Unit Tests (and even to an extend E2E) are getting first-class treatment in Redwood. Here’s what’s coming down the pipeline:

  • a GitHub Action for Redwood CI
  • Diagnostics based on a project reference model
  • A short section in the Tutorial about Adding the GH Action for CI and “hey, here’s how you can run all those tests you didn’t know you have!”
  • A Cookbook doc for Redwood Tests focused on TDD

Good stuff, right? We’d :heart: help with these things, even feedback + input (but no pressure).

The informal meetup sounds good, I am in France so timing might be abit awkward but I will try and join (kids and wife permitting!)

Awesome. Please watch the Forum for more info. And understood about the timezones + general availability. Hoping this will just be the beginning.

1 Like