137 Replies
assume you meant this
was not sure
oh
if you are referncing the same table
yes its a topic a user has made
im new to table design
so maybe im going about it wrong way
but i have users
and i have topics
users create topics
each topic has user_id as owner
user can have many topics
topic can have a parent which is another topic
last part lets you create hierarchies
what i have now
sorry my bad
change to AnySQLiteColumn
instead of Table
gpt-4 said this
ok nice no errors
runnning migration
still hitting this issue
i can get around it but its so strange
you have old drizzle-kit
GitHub
learn-anything/db/package.json at main · learn-anything/learn-anyth...
Organize world's knowledge, explore connections and curate learning paths - learn-anything/db/package.json at main · learn-anything/learn-anything
but that cant be
you need to upgrade it
its all latest modules
unless pnpm messes up
no
it's not latest
ah my bad
i update with
ncu -u
and it did not see thatwhat was the command to db-push to turso
or wait if i run server
it would do it for me i think
GitHub
GitHub - learn-anything/learn-anything: Organize world's knowledge,...
Organize world's knowledge, explore connections and curate learning paths - GitHub - learn-anything/learn-anything: Organize world's knowledge, explore connections and curate learning paths
i dont get why it would run sql in node_modules
is it the schema
you didn;t fix a migration here
It's all explained inside. What was not clear for you? Would love to improve it in a way it would be more clear
i would love if it said it here
beware i couldn't make migration work
do it yourself
and show clear steps
1.
2.
sure!
great
thanks
we will add all of that in this output
does that mean i find query to make it work
then edit migration manually
ALTER TABLE topics ADD
parent_id text REFERENCES topics(id);--> statement-breakpoint
is it a hard thing to automate?
reading on alter table now 🙂
in theory its adding a new column to db
so there are no migrations of data
evenas you can see
we have this statement generated
1 statements that was not generated - was adding FK to an existing column
it's not possible in SQLite in 1 statement
can drizzle create 2 migrations
and still automate it?
so i need to create new migration
0002_hot_rabbit_cooper.sql
or something
edit this too i guess
it;s not a valid syntax
you can't add FK on existin column on SQLite
Sure, it can. We do it in the push command. The case here is that you need to make more than two statements:
You need to rename a table.
Create a new table.
Move all the data.
Remove the old table.
We don't want to be responsible for any of your data movements and manipulation. This is fully your responsibility for your production data. That's why we added links to blog posts for reference. As an improvement, we can generate those statements but still comment them out. In this case, you'll decide what to do with them
makes sense, ok will try make migration manually
is there doc on how to create a migration of drizzle form
with editing of _journal.json
Migrations – DrizzleORM
Drizzle ORM | %s
only mentions generating migrations from changing schema it seems
so you can modify generated sql if it wasn't applied to a database
oh
the flow is
1. You generate an sql file
2. If you see any warnings inside - you can fix them inside this sql file
3. apply migrations
If you still don't have any data inside your db
you can remove all tables and migration folder and generate it from scratch and then apply
for some reason it says generated sql is wrong
if you have data and didn't apply last migration - just modify existing sql file
feel like i do have to study sql in depth after all 😄
EdgeDB | The post-SQL era has arrived
EdgeDB is an open-source database designed as a spiritual successor to SQL and the relational paradigm.
wanted to avoid using it
to keep data at edge
but sql seems tricky
i thought i could do this
extend generated code
with adding of foreign key
but you say thats wrong sql
you can't add FK
to an existing column
SQLite: Foreign Keys
This SQLite tutorial explains how to use Foreign Keys in SQLite with syntax and examples. A foreign key is a way to enforce referential integrity within your SQLite database. A foreign key means that values in one table must also appear in another table.
so i need to do this manually
or somehow encode it in sql
and put in migration
you need to write an sql script
oh i see
inside generated sql file
ok so sql just without add foreign key
ok thanks
actually I would recommend you
to avoid FK's in SQLite
how would i do the use case i need
it super hard to work with them
i need
somehow encode who is parent of who
you don't need to have FK to make joins work
i only have one table
topics
what join
you can just store parent as integer
without reference
the id
it will work the same
ah
smart
and you don't need to handle such migrations
just add index in this field
and you are good to go
so strange
im just modifying the turso drizzle example
i just changed schema
now its 502
this error comes from turso
do you have a data in your database?
i do
should i ask on turso discord?
as you can see
i guess i need to migrate data somehow
you had a ref
you can't remove a FK from an existin column in SQLite
ok
and you can't add a new one
wiping migrations
and you can't change a type is SQLite
fk are no go in sqlite
SQLite can only create and drop tables
and add columns
evertyhing else need to drop a table a create a new one
those are SQLite limitations
was reading about it
like why have fk
it seems they help with joins
I would suggest to just remove all migrations and tables in you db and generate first one from scratch. It will work well
you can have joins without fk
just add index to a column
fk constraints are good to check that you've added an id
did this and wiped migrations folder
it worked, amazing
ah nice
so its literally just a type checker thing
under hood its an integer with id of a table item
Sorry internet went down
To check that you’ve added Id that exists in referenced table
Yeah
So fk constraint will block you if you want to add Id:15, having just 10 topics
Everything else can work the same without fk
so now its on me to make it valid
ok fair enough
doesnt seem too bad
can i make zod somehow
validate if i am passing wrong parent_id
im trying to modify this i guess
so it adds a parent
i used hono briefly before
i assume ctx.req.valid checks if json satisfies
insertTopicSchema
im not sure if it wants camelCase for values
or as they exist in db
it just uses
name
before drizzle you used prisma I imagine?
https://www.npmjs.com/package/drizzle-zod
I tried to use prisma 2 yrs ago. Didn't like it. So we wrote Drizzle for ourselves
and then made it publicly accessible
i tried both
based on schema
the rest should have default values
name, content, ownerId i fill
parent should set to null if not passed
or i guess it needs excplicit null
yea it should be this
mm
I'm not sure
it can be optional
it should
let me check
looking at you error
it says that you've sent ownerId as string
and not number
nothing with drizzle
i dont get logs here
you can add {logger:true}
https://orm.drizzle.team/docs/goodies#logging
It's not a drizzle error
so you should check your code
ok yea it does not reach this ednpoint it seems
will ask in hono discord
its weird
thanks ❤️
like it actually sees the route
app.post("/topics", zValidator("json", insertTopicSchema), async (ctx) => {
ohh
ok solved it
whats the drizzle way of doing seeding of data
i guess i can't directly run that file
i can do something like this
and comment it on/off
strange
like it should have that row there
no errors too
its connected to prod turso
i guess as i dont see insert query here
it ignored it
tried with await too, does not apply
yea it even runs the function, no idea
ohh
i have to commit
ok doing this as it was in example
ok yea that worked