danielrearden / sqlmancer

Conjure SQL from GraphQL queries 🧙🔮✨
https://sqlmancer.netlify.com
MIT License
139 stars 7 forks source link

Allow composite primary keys #82

Open koredefashokun opened 4 years ago

koredefashokun commented 4 years ago

Recently, I created a table to handle a many-to-many relationship. In this case, I want the primary key to be a constrain on multiple columns, but the @model directive only supports a string as the value for the pk argument. I'm not sure what the API would look like, but it would definitely be useful to have the pk field support constrain primary keys.

Great work on this project. It has definitely made it easier for me to handle building my GraphQL server.

danielrearden commented 4 years ago

Thanks for the feature request. This would go hand-in-hand with #53 , although offhand I'm not sure what the best approach would be to changing model methods like findById.

If the table in question doesn't need to be writable, you can utilize a view as a workaround. Either add the view to your database, or use an "inline" one:

@model(
  pk: "pk",
  cte: """
    SELECT
      pk1 || '_' || pk2 as pk,
      another_column,
      yet_another_column
    FROM your_table
  """
)