How to add a new field to post schema

After following the tutorial I wanted to add a new field to the schema.prisma file. I did so with the following code then ran yarn rw db save and then ran yarn rw db up the second command warned me that there will be data loss, and when I said yes anyways I got a prisma error.

 ERROR  Oops, an unexpected error occured!
Failure during a migration command: Connector error. (error: Error querying the da
tabase: Error querying the database: table "new_Post" already exists
   0: migration_core::api::ApplyMigration
           with migration_id="20200609162606-create-posts"
             at migration-engine/core/src/api.rs:77)

schema.prisma

datasource DS {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

generator client {
  provider      = "prisma-client-js"
  binaryTargets = env("BINARY_TARGET")
}

// Define your own datamodels here and run `yarn redwood db save` to create
// migrations for them.

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  body      String
  code      String
  createdAt DateTime @default(now())
}

model Contact {
  id        Int      @id @default(autoincrement())
  name      String
  email     String
  message   String
  createdAt DateTime @default(now())
}

What I want to do eventually is enter a snippet of code have it be formatted in both the admin editor side and on the front end side. I don’t know if I need to do some processing with the code I copy and paste to make sure it get’s certain formatting. I am not sure what even to search for this on npm if there is something that exists already.

The new field I am adding is the code string section

Thanks ahead of time.

Hi @anderskitson happy to help!

There doesn’t seem to be a problem with your schema syntax. Just to confirm, I copied it into a copy of the final tutorial code for testing. I ran yarn rw db save and then yarn rw db up as you did, and it completed without any issues. If you’d like to see my repo, here’s the link: https://github.com/thedavidprice/redwood-tutorial-test

It’s running the latest @redwoodjs packages at v0.10.0

As a next step, try running the db commands with your dev server process stopped (this is the yarn rw dev command). And you might also try deleting any existing Prisma migrations you created along with the local dev.db (which you should save via git commit beforehand in case you need to revert). To do this delete the directory api/prisma/migrations and the file api/prisma/dev.db

Once the Prisma migration correctly runs and is applied to the db, you’ll need to re-create the Post CRUD via the scaffold generator. To do this, run yarn rw g scaffold post --force. This will overwrite an and update the forms to manage posts. Note: you’ll need to remove the re-added Routes, which will be duplicates since you moved the post Routes to /admin/ in the tutorial. Make sense?

Oh, one last request --> could you reply with your system info by running yarn rw info and copying the output here?

Hope this helps! Please let me know either way.

Thanks yes that did help and it is working now, I did have to delete the migrationa and .db file.
And here is my system info if helpful at all

  System:
    OS: macOS 10.15.1
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.3.0 - /var/folders/wl/4sxpd5250vsb67ygf3z0t0r40000gn/T/yarn--1591798901090-0.5717688377317791/node
    Yarn: 1.22.4 - /var/folders/wl/4sxpd5250vsb67ygf3z0t0r40000gn/T/yarn--1591798901090-0.5717688377317791/yarn
  Databases:
    SQLite: 3.28.0 - /usr/bin/sqlite3
  Browsers:
    Chrome: 83.0.4103.97
    Safari: 13.0.3
  npmPackages:
    @redwoodjs/core: ^0.8.2 => 0.8.2
1 Like