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

Infer belongsTo relationships #652

Merged
merged 2 commits into from
Nov 23, 2023
Merged

Infer belongsTo relationships #652

merged 2 commits into from
Nov 23, 2023

Conversation

jasonmccreary
Copy link
Collaborator

@jasonmccreary jasonmccreary commented Nov 23, 2023

After watching Blueprint in action on a Laracast Series, I realized a use case that could be optimized. Blueprint is all about conventions and rapid development, but this video demonstrated some confusion on when to define model columns as well as relationships. Particularly in the case of belongsTo.

Consider the following draft file:

models:
  Conference:
    name: string
    venue_id: unsigned bigInteger
    relationships:
      belongsTo: Venue, Region

In this case a user has defined what they believe to be a foreign key column, but also the belongsTo relationship to Venue. While there are a few ways you may write this, ideally you would do one or the other. Personally, I would not define a belongsTo relationship as simply defining the column is faster (less to type).

For example:

models:
  Conference:
    name: string
    venue_id: id
    region_id: id

However, with this PR, you may also write the following and the foreign key columns will be inferred:

models:
  Conference:
    name: string
    relationships:
      belongsTo: Venue, Region

As a bonus, if you were to write the original draft file, Blueprint will assume the venue_id is meant to be a data type of id.


As a development note to myself and future contributors, these types of adjustments should be done in the lexer. Doing these kinds of things higher up avoid having to do it in several of the underlying generators.

@jasonmccreary jasonmccreary merged commit 716e3e9 into master Nov 23, 2023
8 checks passed
@jasonmccreary jasonmccreary deleted the infer-belongsto branch November 23, 2023 23:16
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

Successfully merging this pull request may close these issues.

2 participants