© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
1 reply
Gary, el Pingüino Artefacto

Get table definition?

How can I get a SQL to create a table based on a table.

For example:
const files = pgTable(
  "files",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    createdAt: timestamp("created_at", { mode: "date" }).notNull().defaultNow(),
    url: text("url").notNull(),
    size: decimal("size"),
    contentType: text("content_type"),
    originalName: text("original_name"),
    path: text("path").notNull().default(""),
  },
  (table) => ({
    pathIdx: index().on(table.path),
  })
)


const definition = getTableDefinition(files);
const files = pgTable(
  "files",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    createdAt: timestamp("created_at", { mode: "date" }).notNull().defaultNow(),
    url: text("url").notNull(),
    size: decimal("size"),
    contentType: text("content_type"),
    originalName: text("original_name"),
    path: text("path").notNull().default(""),
  },
  (table) => ({
    pathIdx: index().on(table.path),
  })
)


const definition = getTableDefinition(files);


Definition should be something like this:
CREATE TABLE IF NOT EXISTS "files" (
    "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
    "created_at" timestamp DEFAULT now() NOT NULL,
    "url" text NOT NULL,
    "size" numeric,
    "content_type" text,
    "original_name" text,
    "path" text DEFAULT '' NOT NULL
);
CREATE TABLE IF NOT EXISTS "files" (
    "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
    "created_at" timestamp DEFAULT now() NOT NULL,
    "url" text NOT NULL,
    "size" numeric,
    "content_type" text,
    "original_name" text,
    "path" text DEFAULT '' NOT NULL
);
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

all table definition type
Drizzle TeamDTDrizzle Team / help
3y ago
Base Schema / Table definition
Drizzle TeamDTDrizzle Team / help
3y ago
Table definition to TS type
Drizzle TeamDTDrizzle Team / help
2y ago
Table definition has 'any' type
Drizzle TeamDTDrizzle Team / help
3y ago