Ā© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•12mo ago•
1 reply
Myst

Dynamic Table Names

I am new to drizzle (started today) and am wondering if it is possible to have a dynamic table name. I have a database that has a new table created for each month. The schema of all these tables is the same. Is it possible to just have 1 schema definition instead of needing to have this repeated? I was able to accomplish this in the past using sqlalchemy using the blow config. Sorry if I have asked this in a confusing manner.

Current Drizzle:
export const sqltData1202401 = pgTable(
  "sqlt_data_1_2024_01",
  {
    tagid: integer().notNull(),
    // You can use { mode: "bigint" } if numbers are exceeding js number limitations
    intvalue: bigint({ mode: "number" }),
    /// other variables
  },
  (table) => [
    index("sqlt_data_1_2024_01t_stampndx").using(
      "btree",
      table.tStamp.asc().nullsLast().op("int8_ops")
    ),
    primaryKey({
      columns: [table.tagid, table.tStamp],
      name: "sqlt_data_1_2024_01_pkey",
    }),
  ]
);

export const sqltData1202402 = pgTable(
  "sqlt_data_1_2024_02",
  {
    tagid: integer().notNull(),
    // You can use { mode: "bigint" } if numbers are exceeding js number limitations
    intvalue: bigint({ mode: "number" }),
    /// other variables
  },
  (table) => [
    index("sqlt_data_1_2024_02t_stampndx").using(
      "btree",
      table.tStamp.asc().nullsLast().op("int8_ops")
    ),
    primaryKey({
      columns: [table.tagid, table.tStamp],
      name: "sqlt_data_1_2024_02_pkey",
    }),
  ]
);
export const sqltData1202401 = pgTable(
  "sqlt_data_1_2024_01",
  {
    tagid: integer().notNull(),
    // You can use { mode: "bigint" } if numbers are exceeding js number limitations
    intvalue: bigint({ mode: "number" }),
    /// other variables
  },
  (table) => [
    index("sqlt_data_1_2024_01t_stampndx").using(
      "btree",
      table.tStamp.asc().nullsLast().op("int8_ops")
    ),
    primaryKey({
      columns: [table.tagid, table.tStamp],
      name: "sqlt_data_1_2024_01_pkey",
    }),
  ]
);

export const sqltData1202402 = pgTable(
  "sqlt_data_1_2024_02",
  {
    tagid: integer().notNull(),
    // You can use { mode: "bigint" } if numbers are exceeding js number limitations
    intvalue: bigint({ mode: "number" }),
    /// other variables
  },
  (table) => [
    index("sqlt_data_1_2024_02t_stampndx").using(
      "btree",
      table.tStamp.asc().nullsLast().op("int8_ops")
    ),
    primaryKey({
      columns: [table.tagid, table.tStamp],
      name: "sqlt_data_1_2024_02_pkey",
    }),
  ]
);


SQLAlchemy:
def get_data_table(table_name: str | Column[str]):
    DynamicBase = declarative_base(class_registry=dict())

    class DataTable(DynamicBase):
        __tablename__ = table_name

        tagid = Column(Integer, primary_key=True)
        intvalue = Column(BigInteger)
        #other variables

    return DataTable
def get_data_table(table_name: str | Column[str]):
    DynamicBase = declarative_base(class_registry=dict())

    class DataTable(DynamicBase):
        __tablename__ = table_name

        tagid = Column(Integer, primary_key=True)
        intvalue = Column(BigInteger)
        #other variables

    return DataTable
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

dynamic table name
Drizzle TeamDTDrizzle Team / help
3y ago
extract table names from db.query
Drizzle TeamDTDrizzle Team / help
3y ago
Dynamic function get use typed column names using getTableConfig
Drizzle TeamDTDrizzle Team / help
2y ago
NaN primary keys + not honouring camelCase table names
Drizzle TeamDTDrizzle Team / help
2y ago