Skip to content
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

Feature Request: Foreign Key constraint option in schema types #787

Open
karai17 opened this issue Jun 5, 2024 · 0 comments
Open

Feature Request: Foreign Key constraint option in schema types #787

karai17 opened this issue Jun 5, 2024 · 0 comments

Comments

@karai17
Copy link

karai17 commented Jun 5, 2024

From my understanding, Lapis' scheme type foreign_key is just an alias for integer and does not create an actual constraint. the only way to add a foreign key constraint to the database using Lapis' migration and db utilities is to manually add the constraint as an sql fragement:

schema.create_table("comments", {
  { "id",       types.integer { unique=true, primary_key=true }},
  { "post_id",  types.integer },
  { "body",     types.text },

  "FOREIGN KEY (post_id) REFERENCES posts"
})

However, given that there is already a primary_key option, a foreign_key option makes sense:

schema.create_table("comments", {
  { "id",       types.integer { unique=true, primary_key=true }},
  { "post_id",  types.integer { foreign_key="posts" }}, -- if foreign_key is boolean TRUE, infer table from column name: post_id -> posts
  { "body",     types.text }
})

I assume that using primary_key on multiple columns correctly combines them into a composite key, so doing the same with foreign_key should work the same way. If this is not the case and you must use an sql fragment to create composite keys, then feature parity makes sense in that regard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant