If you have a database you’d like to use as the foundation for your new resource you can upload a schema.prisma file based on your database. Amplication will instantly create entities, fields, and relationships based on your schema, significantly accelerating your development process and letting you focus on business logic, not database setup. This feature quickly translates your existing database structure into Amplication’s internal model, giving you a ready-to-go starting point.

The Upload Schema feature is powered by Prisma’s Introspection CLI. Learn more about it on Prisma’s docs. Also, this feature is only available when using the .NET or Node.js blueprints in Amplication.

Overview

When you upload a Prisma schema, Amplication inspects your existing database structure and generates corresponding entities, fields, and relationships. By standardizing and automating the process, Amplication helps you maintain consistency and best practices across your organization, saving significant time and reducing technical debt.

Run the Introspection Process

To generate a Prisma schema from your existing database, you’ll use Prisma’s introspection feature.

1

Install Prisma CLI

If you haven’t already, install the Prisma CLI globally:

npm install -g prisma
2

Create a Prisma Schema File

Create a file named schema.prisma in your project. In this file, set the url in the datasource block to point to your existing database:

schema.prisma
datasource db {
  provider = "postgresql"
  url      = "postgresql://janedoe:mypassword@localhost:5432/mydb?schema=sample"
}

Adjust the URL according to your database provider (e.g., PostgreSQL, MySQL, SQL Server).

3

Run Prisma Introspection

Navigate to the directory with your schema.prisma file, then run:

prisma db pull

This inspects your database and generates models within schema.prisma.

4

Validate Your Generated Schema

Open schema.prisma in a text editor to ensure it’s error-free and follows your organization’s naming conventions or best practices. If you notice issues (e.g., missing relations or improperly named fields), make the necessary adjustments.

If the introspection results in errors or incomplete models, refer to Prisma’s Error Reference for guidance.

Create Your Initial Migration (Optional)

If you plan to use Prisma Migrate on your newly introspected database, consider “baselining” your database to align existing data with future migrations.

1

Initialize a Migrations Folder

Create a directory for your initial migration, for example:

mkdir -p prisma/migrations/init_after_introspection
2

Generate a Baseline Migration Script

In the same directory as your schema.prisma, run:

prisma migrate diff \
  --from-empty \
  --to-schema-datamodel prisma/schema.prisma \
  --script > prisma/migrations/init_after_introspection/migration.sql

Review the generated migration.sql to ensure accuracy.

3

Apply the Baseline Migration

Mark your newly created migration as already applied:

prisma migrate resolve --applied init_after_introspection

This step records the migration in the _prisma_migrations table without making changes.

Upload Your Prisma Schema File to Amplication

Once your schema.prisma file is ready, upload it to Amplication from the Resources section of your service.

1

Open Your Resource in Amplication

Locate the service resource where you want to import your database schema.

2

Navigate to Entities

Click on the “Entities” tab in the navigation bar of your resource to access the entities page.

3

Click 'Upload Schema'

Select Upload Schema on the top-right corner of the page.

4

Attach Your File

Upload your schema.prisma file using the provided form.

If the uploaded schema is invalid or lacks identifiable entities, Amplication will halt the process and display an error. Ensure your schema.prisma file is valid before proceeding.

Check the Conversion Logs

After uploading, Amplication displays a log detailing the conversion of your Prisma models into entities. Review these logs to confirm that:

  • All expected models are detected.
  • Fields and relationships match your existing database structure.
  • No warnings require further action.

Schema uploads can’t be redone in the same project. Ensure your schema and logs are correct before proceeding.

Review Your Newly Created Entities

Once conversion completes successfully, Amplication creates entities and fields corresponding to your models. You can visualize these in the ERD view to confirm relationships align with your database design.

Example ERD view after a successful schema upload

Apply Your Migration to the New Amplication Service

If you generated baseline migrations earlier or have an existing migration history, you can now merge that with your new Amplication resource:

1

Commit Changes

Commit the newly generated entities and configuration to your preferred Git repository.

2

Update Environment Variables

In your .env file (or your deployment environment), set the DB_URL to your existing database’s connection string.

3

Copy Migrations Folder

Move or copy the previously created migrations folder into the prisma directory of your new Amplication service repository.

4

Run Database Commands

From within your service’s root directory, install dependencies and generate your Prisma client:

npm i
npm run prisma:generate
npm run db:migrate-save

This aligns your Amplication resource with your existing database.

Next Steps

By uploading your schema into Amplication, you jumpstart development with organization-wide standards and ensure your service remains aligned with evolving best practices, saving you time and reducing maintenance overhead.