src/content/docs/schemas.mdx
import Tab from '@mdx/Tab.astro'; import Tabs from '@mdx/Tabs.astro'; import IsSupportedChipGroup from '@mdx/IsSupportedChipGroup.astro'; import Section from '@mdx/Section.astro'; import Callout from '@mdx/Callout.astro';
<IsSupportedChipGroup chips={{ 'PostgreSQL': true, 'MySQL': true, 'SQLite': false, 'SingleStore': true, 'MSSQL': true, 'CockroachDB': true }} />
If you declare an entity within a schema, query builder will prepend schema names in queries:
select * from "schema"."users"
<Tabs items={['PostgreSQL', 'MySQL', "SQLite", "SingleStore", 'MSSQL', 'CockroachDB']}> <Tab> <Section> ```ts copy {3,5,7} import { serial, text, pgSchema } from "drizzle-orm/pg-core";
export const mySchema = pgSchema("my_schema");
export const colors = mySchema.enum('colors', ['red', 'green', 'blue']);
export const mySchemaUsers = mySchema.table('users', {
id: serial('id').primaryKey(),
name: text('name'),
color: colors('color').default('red'),
});
```
```sql
CREATE SCHEMA "my_schema";
CREATE TYPE "my_schema"."colors" AS ENUM ('red', 'green', 'blue');
CREATE TABLE "my_schema"."users" (
"id" serial PRIMARY KEY,
"name" text,
"color" "my_schema"."colors" DEFAULT 'red'
);
```
</Section>
export const mySchema = mysqlSchema("my_schema")
export const mySchemaUsers = mySchema.table("users", {
id: int("id").primaryKey().autoincrement(),
name: text("name"),
});
```
```sql
CREATE SCHEMA "my_schema";
CREATE TABLE "my_schema"."users" (
"id" serial PRIMARY KEY,
"name" text
);
```
</Section>
export const mySchema = singlestoreSchema("my_schema")
export const mySchemaUsers = mySchema.table("users", {
id: int("id").primaryKey().autoincrement(),
name: text("name"),
});
```
```sql
CREATE SCHEMA "my_schema";
CREATE TABLE "my_schema"."users" (
"id" serial PRIMARY KEY,
"name" text
);
```
</Section>
export const mySchema = mssqlSchema("my_schema")
export const mySchemaUsers = mySchema.table("users", {
id: int().primaryKey(),
name: text(),
});
```
```sql
CREATE SCHEMA [my_schema];
CREATE TABLE [my_schema].[users] (
[id] int PRIMARY KEY,
[name] text
);
```
</Section>
export const mySchema = cockroachSchema("my_schema");
export const colors = mySchema.enum('colors', ['red', 'green', 'blue']);
export const mySchemaUsers = mySchema.table('users', {
id: int4().primaryKey(),
name: text(),
color: colors().default('red'),
});
```
```sql
CREATE SCHEMA "my_schema";
CREATE TYPE "my_schema"."colors" AS ENUM ('red', 'green', 'blue');
CREATE TABLE "my_schema"."users" (
"id" serial PRIMARY KEY,
"name" text,
"color" "my_schema"."colors" DEFAULT 'red'
);
```
</Section>
{/* TODO: ??? example > Warning
If you will have tables with same names in different schemas then drizzle will respond with
never[]error in result types and error from databaseIn this case you may use alias syntax */}