-
-
Notifications
You must be signed in to change notification settings - Fork 814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG]: SQL Generation - Views with Dependencies Created in Alphabetical Order Instead of Schema Definition Order #4076
Comments
Additional Finding: View Creation Order is UnpredictableAfter further investigation, I discovered that the view creation order isn't even following alphabetical order as initially thought. Even when adding numerical prefixes to force ordering, the generation still produces views in an unpredictable order. ExampleUsing numbered prefixes in schema: export const userTokenUsageView = mysqlView('token_usage_01_user')...
export const teamTokenUsageView = mysqlView('token_usage_02_team')... Generated SQL still puts the dependent view first: VIEW `token_usage_02_team` AS (
// References token_usage_01_user which doesn't exist yet
select ... from `token_usage_01_user` ...
);--> statement-breakpoint
VIEW `token_usage_01_user` AS (
// Base view definition
select ... from `chat_messages` ...
); This means:
This makes the issue more critical as there currently appears to be no reliable workaround except writing raw SQL migrations. |
This is also an issue with generated migrations for Postgres. I always end up needing to modify the migrations file manually to drop views and add them in the right order. |
@juanvilladev I have no idea why Drizzle Kit team decided to generate the SQL migrations in alphabetical order instead of following the definition order in the schema file. My current solution for this issue is define the view using raw SQL code then create a view in the schema with |
@kei-ichi can you perhaps show an example please? Unsure exactly what you mean by this. |
@juanvilladev
import { sql } from 'drizzle-orm'
await db.execute(sql`SQL TO CREATE VIEW HERE`);
export const dummyView = pgView("dummy_view", {
id: serial("id"),
name: text("name"),
email: text("email"),
}).existing();
With |
Report hasn't been filed before.
What version of
drizzle-orm
are you using?0.39.1
What version of
drizzle-kit
are you using?0.30.4
Other packages
No response
Describe the Bug
Issue Description
When generating SQL migrations, Drizzle Kit creates tables and views in alphabetical order rather than following the schema definition order. This causes issues particularly with views that depend on other views.
Example Case
In our schema, we have two views where one depends on another:
However, the generated SQL creates them in alphabetical order:
Current Behavior:
Expected Behavior:
Workaround
Currently, we need to either:
Impact
This issue affects any schema where:
The text was updated successfully, but these errors were encountered: