© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•10mo ago•
1 reply
iamgengar.

How to get schema name from table name?

I use the table as input for a function, and we can get the table name with
getTableName
getTableName
which is no issue. However for using
db.query.<schema name>
db.query.<schema name>
we need the schema name (i.e. the js variable name), not the table name, but the table itself doesn't know it's variable name.

Is there an easy way to retrieve it?

Context:
I want to make a function that takes a table and some columns and returns the query only taking those columns. I've tried it with
db.select()
db.select()
, however i've found it quite cumbersome to make such a function for it:
- The select syntax is more bloated, and more annoying to convert to. e.g. why enter:
{
  field1: users.id,
  field2: users.name,
}
{
  field1: users.id,
  field2: users.name,
}

instead of the
db.query
db.query
which would need:

{
  id: true,
  name: true,
}
{
  id: true,
  name: true,
}

And we can even easily convert
['id', 'name']
['id', 'name']
to that format internally. Hence we thought the
db.query
db.query
would be a better choice here. However the issue is that using
db.query.<schema name>
db.query.<schema name>
needs the schema name, not the table name. And that schema name... is more difficult to get (especially in a generic way).

We could make a mapping once based on the schema of the application (here
appSchema
appSchema
), however then we hardcode the schema in there, which makes it more difficult to use in generic functionality (see first comment with code, otherwise post it to long).

If anyone know a good solution, we're trying to make generic crud endpoints, and would like the dev's to specify which columns should be returned. This while all being 100% typed.
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

How to get column names from Drizzle Table?
Drizzle TeamDTDrizzle Team / help
2y ago
How to get the name of the table
Drizzle TeamDTDrizzle Team / help
3y ago
Get `CREATE TABLE ...` SQL from a schema
Drizzle TeamDTDrizzle Team / help
14mo ago
How to `as` a table name
Drizzle TeamDTDrizzle Team / help
17mo ago