Skip to content

Commit

Permalink
Merge branch 'main' into prisma-migrate-hybrid
Browse files Browse the repository at this point in the history
  • Loading branch information
ruheni authored Nov 21, 2023
2 parents 58c50e0 + 7e04282 commit 0e24a99
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,23 @@ Implicit m-n relations:

If you obtain your data model from [introspection](/concepts/components/introspection), you can still use implicit m-n-relations by following Prisma's [conventions for relation tables](#conventions-for-relation-tables-in-implicit-m-n-relations). The following example assumes you want to create a relation table to get an implicit m-n-relation for two models called `Post` and `Category`.

##### Table names
##### Relation table

If you want a relation table to be picked up by introspection as an implicit m-n-relation, the name must follow this exact structure:

- It must start with an underscore `_`
- Then the name of the first table in alphabetical order (in this case `Category`)
- Then the name of the first model in alphabetical order (in this case `Category`)
- Then `To`
- Then the name of the second table in alphabetical order (in this case `Post`)
- Then the name of the second model in alphabetical order (in this case `Post`)

In the example, the correct table name is `_CategoryToPost`.

When creating an implicit m-n-relation yourself in the Prisma schema file, you can [configure the relation](#configuring-the-name-of-the-relation-table-in-implicit-many-to-many-relations) to have a different name. This will change the name given to the relation table in the database. For example, for a relation named `"MyRelation"` the corresponding table will be called `_MyRelation`.

###### Multi-schema

If your implicit many-to-many relationship spans multiple database schemas (using the [`multiSchema` preview feature](/guides/database/multi-schema)), the relation table (with the name defined directly above, in the example `_CategoryToPost`) must be present in the same database schema as the first model in alphabetical order (in this case `Category`).

##### Columns

A relation table for an implicit m-n-relation must have exactly two columns:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
title: '.env files'
metaTitle: '.env files'
metaDescription: 'Configure environment variables using .env files in Prisma'
---

<TopBlock>

Prisma creates a default `.env` file at your projects root. You can choose to replace this file or create a new one in the `prisma` folder, or if you choose to relocate your `prisma.schema` file, alongside that.

</TopBlock>

### <inlinecode>.env</inlinecode> file locations

The Prisma CLI looks for `.env` files, in order, in the following locations:

1. In the root folder of your project (`./.env`)
1. From the same folder as the schema specified by the `--schema` argument
1. From the same folder as the schema taken from `"prisma": {"schema": "/path/to/schema.prisma"}` in `package.json`
1. From the `./prisma` folder

If a `.env` file is located in step 1., but additional, clashing `.env` variables are located in steps 2. - 4., the CLI will throw an error. For example, if you specify a `DATABASE_URL` variable in two different `.env` files, you will get the following error:

```
Error: There is a conflict between env vars in .env and prisma/.env
Conflicting env vars:
DATABASE_URL
We suggest to move the contents of prisma/.env to .env to consolidate your env vars.
```

The following table describes where the Prisma CLI looks for the `.env` file:

| **Command** | **Schema file location** | **`.env` file locations checked, in order** |
| :---------------------------------------------- | :--------------------------------------------------------------------------- | :-------------------------------------------------------- |
| `prisma [command]` | `./prisma/schema.prisma` | `./.env`<br />`./prisma/.env` |
| `prisma [command] --schema=./a/b/schema.prisma` | `./a/b/schema.prisma` | `./.env`<br />`./a/b/.env`<br />`./prisma/.env` |
| `prisma [command]` | `"prisma": {"schema": "/path/to/schema.prisma"}` | `.env`<br /> `./path/to/schema/.env`<br />`./prisma/.env` |
| `prisma [command]` | No schema (for example, when running `prisma db pull` in an empty directory) | `./.env`<br />`./prisma/.env` |

Any environment variables defined in that `.env` file will automatically be loaded when running a Prisma CLI command.

<Admonition type="warning">

**Do not commit your `.env` files into version control**!

</Admonition>

Refer to the `dotenv` documentation for information about [what happens if an environment variable is defined in two places](https://www.npmjs.com/package/dotenv#what-happens-to-environment-variables-that-were-already-set).

### Expanding variables

Variables stored in `.env` files can be expanded using the format specified by [dotenv-expand](https://github.com/motdotla/dotenv-expand).

```env file=.env
DATABASE_URL=postgresql://test:test@localhost:5432/test
DATABASE_URL_WITH_SCHEMA=${DATABASE_URL}?schema=public
```

This will make the environment variable `DATABASE_URL_WITH_SCHEMA` with value `postgresql://test:test@localhost:5432/test?schema=public` available for Prisma.

You can also use environment variables in the expansion that are set _outside_ of the `.env` file, for example a database URL that is set on a PaaS like Heroku or similar:

```terminal
# environment variable already set in the environment of the system
export DATABASE_URL=postgresql://test:test@localhost:5432/test
```

```env file=.env
DATABASE_URL_WITH_SCHEMA=${DATABASE_URL}?schema=foo
```

This will make the environment variable `DATABASE_URL_WITH_SCHEMA` with value `postgresql://test:test@localhost:5432/test?schema=foo` available for Prisma.

### Example: Set the `DATABASE_URL` environment variable in an `.env` file

It is common to load your database connection URL from an environment variable:

```prisma
// schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
```

You can set the `DATABASE_URL` in your `.env` file:

```env file=.env
DATABASE_URL=postgresql://test:test@localhost:5432/test?schema=public
```

When you run a command that needs access to the database defined via the `datasource` block (for example, `prisma db pull`), the Prisma CLI automatically loads the `DATABASE_URL` environment variables from the `.env` file and makes it available to the CLI.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ The environment variable belongs to the environment where a process is running.

Taking the `TEMP` environment variable as an example, one can query its value to find where to store temporary files. This is a system environment variable and can be queried by any process or application running on the machine.

Any program can read and create these environment variables. They are a cheap and effective way to store simple information.

</TopBlock>

## How does Prisma use environment variables?
Expand All @@ -33,92 +35,6 @@ Looking to use more than one `.env` file? See [Using multiple `.env` files](./us

</Admonition>

## Using <inlinecode>.env</inlinecode> files

Prisma creates a default `.env` file at your projects root. You can choose to replace this file or create a new one in the `prisma` folder, or if you choose to relocate your `schema.prisma` file, alongside that.

### `.env` file locations

The Prisma CLI looks for `.env` files, in order, in the following locations:

1. In the root folder of your project (`./.env`)
1. From the same folder as the schema specified by the `--schema` argument
1. From the same folder as the schema taken from `"prisma": {"schema": "/path/to/schema.prisma"}` in `package.json`
1. From the `./prisma` folder

If a `.env` file is located in step #1, but additional, clashing `.env` variables are located in steps #2 - 4, the CLI will throw an error. For example, if you specify a `DATABASE_URL` variable in two different `.env` files, you will get the following error:

```
Error: There is a conflict between env vars in .env and prisma/.env
Conflicting env vars:
DATABASE_URL
We suggest to move the contents of prisma/.env to .env to consolidate your env vars.
```

The following table describes where the Prisma CLI looks for the `.env` file:

| **Command** | **Schema file location** | **`.env` file locations checked, in order** |
| :---------------------------------------------- | :--------------------------------------------------------------------------- | :-------------------------------------------------------- |
| `prisma [command]` | `./prisma/schema.prisma` | `./.env`<br />`./prisma/.env` |
| `prisma [command] --schema=./a/b/schema.prisma` | `./a/b/schema.prisma` | `./.env`<br />`./a/b/.env`<br />`./prisma/.env` |
| `prisma [command]` | `"prisma": {"schema": "/path/to/schema.prisma"}` | `.env`<br /> `./path/to/schema/.env`<br />`./prisma/.env` |
| `prisma [command]` | No schema (for example, when running `prisma db pull` in an empty directory) | `./.env`<br />`./prisma/.env` |

Any environment variables defined in that `.env` file will automatically be loaded when running a Prisma CLI command.

<Admonition type="warning">

**Do not commit your `.env` files into version control**!

</Admonition>

Refer to the `dotenv` documentation for information about [what happens if an environment variable is defined in two places](https://www.npmjs.com/package/dotenv#what-happens-to-environment-variables-that-were-already-set).

### Expanding variables

Variables stored in `.env` files can be expanded using the format specified by [dotenv-expand](https://github.com/motdotla/dotenv-expand).

```env file=.env
DATABASE_URL=postgresql://test:test@localhost:5432/test
DATABASE_URL_WITH_SCHEMA=${DATABASE_URL}?schema=public
```

This will make the environment variable `DATABASE_URL_WITH_SCHEMA` with value `postgresql://test:test@localhost:5432/test?schema=public` available for Prisma.

You can also use environment variables in the expansion that are set _outside_ of the `.env` file, for example a database URL that is set on a PaaS like Heroku or similar:

```terminal
# environment variable already set in the environment of the system
export DATABASE_URL=postgresql://test:test@localhost:5432/test
```

```env file=.env
DATABASE_URL_WITH_SCHEMA=${DATABASE_URL}?schema=foo
```

This will make the environment variable `DATABASE_URL_WITH_SCHEMA` with value `postgresql://test:test@localhost:5432/test?schema=foo` available for Prisma.

### Example: Set the `DATABASE_URL` environment variable in an `.env` file

It is common to load your database connection URL from an environment variable:

```prisma
// schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
```

You can set the `DATABASE_URL` in your `.env` file:

```env file=.env
DATABASE_URL=postgresql://test:test@localhost:5432/test?schema=public
```

When you run a command that needs access to the database defined via the `datasource` block (for example, `prisma db pull`), the Prisma CLI automatically loads the `DATABASE_URL` environment variables from the `.env` file and makes it available to the CLI.

### Using environment variables in your code

If you want environment variables to be evaluated at runtime, you need to load them manually in your application code (for example, by using [`dotenv`](https://github.com/motdotla/dotenv)):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,31 +176,38 @@ A `generator` block accepts the following fields:

The following tables list all supported operating systems with the name of platform to specify in [`binaryTargets`](/concepts/components/prisma-schema/generators#binary-targets).

Unless specified otherwise, the default supported CPU architecture is x86_64.

##### macOS

| Build OS | Prisma engine build name |
| :-------------- | :----------------------- |
| macOS Intel x86 | `darwin` |
| macOS ARM64 | `darwin-arm64` |
| Build OS | Prisma engine build name |
| :----------------- | :----------------------- |
| macOS Intel x86_64 | `darwin` |
| macOS ARM64 | `darwin-arm64` |

##### Windows

| Build OS | Prisma engine build name |
| :------- | :----------------------- |
| Windows | `windows` |

##### Linux (Alpine)
##### Linux (Alpine on x86_64 architectures)

| Build OS | Prisma engine build name | OpenSSL |
| :------------------------------ | :----------------------------------- | :-----: |
| Alpine (3.16 and older), x86_64 | `linux-musl` | 1.1.x |
| Alpine (3.17 and newer), x86_64 | `linux-musl-openssl-3.0.x`\* | 3.0.x |
| Alpine (3.16 and older), ARM64 | `linux-musl-arm64-openssl-1.1.x`\*\* | 1.1.x |
| Alpine (3.17 and newer), ARM64 | `linux-musl-arm64-openssl-3.0.x`\*\* | 3.0.x |
| Build OS | Prisma engine build name | OpenSSL |
| :------------------ | :--------------------------- | :-----: |
| Alpine (3.17 and newer) | `linux-musl-openssl-3.0.x`\* | 3.0.x |
| Alpine (3.16 and older) | `linux-musl` | 1.1.x |

\* Available in Prisma versions 4.8.0 and later.

\*\* Available in Prisma versions 4.10.0 and later.
##### Linux (Alpine on ARM64 architectures)

| Build OS | Prisma engine build name | OpenSSL |
| :------------------ | :--------------------------------- | :-----: |
| Alpine (3.17 and newer) | `linux-musl-arm64-openssl-3.0.x`\* | 3.0.x |
| Alpine (3.16 and older) | `linux-musl-arm64-openssl-1.1.x`\* | 1.1.x |

\* Available in Prisma versions 4.10.0 and later.

##### Linux (Debian), x86_64

Expand Down Expand Up @@ -259,7 +266,7 @@ The following tables list all supported operating systems with the name of platf
| Arch Linux 2019.09.01 | `debian-openssl-1.1.x` | 1.1.x |
| Arch Linux 2023.04.23 | `debian-openssl-3.0.x` | 3.0.x |

##### Linux (most distributions except Alpine), ARM64
##### Linux ARM64 (all major distros but Alpine)

| Build OS | Prisma engine build name | OpenSSL |
| :----------------------------- | :-------------------------- | :-----: |
Expand Down

0 comments on commit 0e24a99

Please sign in to comment.