2 tables cannot reference each others columns?
I have the following tables declaration:
As you can see, for some reason I CANNOT reference one table's column from the other table, while this table's column is already referenced by the other table. Can you explain why so and what should I do?
7 Replies
Use relations instead: https://orm.drizzle.team/docs/rqb#declaring-relations
@GetPsyched I use relations... I need to store the id of the correct answer anyway
not asking you to not store the id
just link the id using relations
Hello, @piscopancer! Could you please clarify what you want to accomplish? What relations do you have between tables? I see that you have
acceptedAnswerId
, one question can have several answers, but answer can have only one question, is it right?yes, I am building a QA service where a user ask a question, receives a bunch of answers from other users and picks one which they think is correct or satisfies them. So, one
question
has many answers
and the accepted_answer_id
(1,2,3,4... | null) while answers
have a question_id
column on which they exist.
I was having a lively discussion on this issue on a typescript server and we figured out that the error I was getting was related to typescript itself being unable to infer the types of tables that mutually referenced each other, resulting in both of them being of type any
(which sucks). For that a solution was found. See SQLiteColumn
explicit type declaration
This fixes the type inference, it's us helping typescript, possibly...
Anyway, odds are this issue is totally on the behalf of how typescript was used by the drizzle developers to help it infer types, which is not perfect, as you can seeOk, cool, this solution fits. Btw, you can use
AnySQLiteColumn
. Also, did you think about changing your table structure a bit?
You have one-to-many
relation. So, one question has got many answers and answer has only one question, so you can do smth like this:
or this solution doesn't fit well for your case?And I will also have to apply
false
for the rest of the answers, if the user had previously marked one answer as true
and then decided that there is a more fitting answer. Isn't it weird?
there cannot be a chicken and egg problem. First comes the question, second comes the answer because every answer has a required foreign key question_id
meanwhile a question
can be on its own
question
's accepted_answer_id
is nullable!!! which means it is NULL upon creation