ansipedantic
DTDrizzle Team
•Created by ansipedantic on 9/2/2024 in #help
One to Many with composite primary key?
I am working on a transit-related application (not super important, but may provide context for mental model of the below tables). I am importing data that is conforming to the
gtfs
spec, so I am not doing any pre/post processing of the data provided in a collection of csv files, I am just importing them straight into a database.
I have a table of "Shapes" (which really should be though of as points (lat/lon) along a bus trip). The primary key is a combination of the id
and sequence
fields.
I also have a table of "Trips" which contains a shape_id
in the table. Different trips may have the same shape_id
(e.g. think of trips happening at different times).
Because the Shapes
table has a composite primary key, I cannot figure out how to create a relation from the Trips
table to successfully query the shape rows for a particular trip.
While it's true that each shape_id has many trips, I would probably never need to look that up, but I can still say something like
But where I'm running into trouble is how to create the relationship for Trips
to get the shapes[] results
I suppose in the worst case I would have to manually create a many-to-many join table between trips and shapes and use the typical many-to-many pattern, but I would love to avoid that if there is some mechanism to achieve the above.
The join table would essentially be
with no repeating trip_id
(which isn't much of a join table).
Anyway, I know this was a lot of text, but I hope it is sufficient information to explain what I am trying to accomplish.1 replies
DTDrizzle Team
•Created by ansipedantic on 12/21/2023 in #help
Migrations with new notNull() columns
what's the proper way to add a notNull() column through a migration? I just tried to add a column in my schema like
and generated the migration with the standard
npx drizzle-kit generate:pg
command
but during the await migrate(...)
call, I get an error that existing rows have a NULL value (which, yes... i'm just adding the column now). I don't see anything in the docs about customizing migrations to backfill existing rows in cases like this. Is there a way to properly handle this?10 replies