Back to Drizzle Orm

UpdateSchema

src/mdx/get-started/mysql/UpdateSchema.mdx

latest802 B
Original Source

If you want to update your table schema, you can do it in the schema.ts file. For example, let's add a new column phone to the users_table:

typescript
import {
  mysqlTable,
  mysqlSchema,
  AnyMySqlColumn,
  primaryKey,
  unique,
  serial,
  varchar,
  int,
} from 'drizzle-orm/mysql-core';
import { sql } from 'drizzle-orm';

export const usersTable = mysqlTable(
  'users_table',
  {
    id: serial().notNull(),
    name: varchar({ length: 255 }).notNull(),
    age: int().notNull(),
    email: varchar({ length: 255 }).notNull(),
    phone: varchar({ length: 255 }),
  },
  (table) => [
    primaryKey({ columns: [table.id], name: 'users_table_id' }),
    unique('id').on(table.id),
    unique('users_table_email_unique').on(table.email),
  ],
);