Back to Drizzle Orm

IntrospectMSSQL

src/mdx/get-started/mssql/IntrospectMSSQL.mdx

latest1.3 KB
Original Source

Drizzle Kit provides a CLI command to introspect your database and generate a schema file with migrations. The schema file contains all the information about your database tables, columns, relations, and indices.

For example, you have such table in your database:

sql
CREATE TABLE [users] (
 [id] int,
 [name] varchar(255) NOT NULL,
 [age] int NOT NULL,
 [email] varchar(255) NOT NULL,
 CONSTRAINT [users_pkey] PRIMARY KEY([id]),
 CONSTRAINT [users_email_key] UNIQUE([email])
);

Pull your database schema:

bash
npx drizzle-kit pull --init

The result of introspection will be a schema.ts file, meta folder with snapshots of your database schema, sql file with the migration and relations.ts file for relational queries.

Here is an example of the generated schema.ts file:

typescript
// table schema generated by introspection
import { mssqlTable, int, varchar, unique, primaryKey } from "drizzle-orm/mssql-core"

export const users = mssqlTable("users", {
 id: int(),
 name: varchar({ length: 255 }).notNull(),
 age: int().notNull(),
 email: varchar({ length: 255 }).notNull(),
}, (table) => [
 primaryKey({ columns: [table.id], name: "users_pkey"}),
 unique("users_email_key").on(table.email)
]);

Learn more about introspection in the documentation.