feat: add ForeignID, ForeignUlid, and ForeignUuid methods to Blueprint for migration #604#1187
Conversation
…lueprint` for migration goravel#604
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1187 +/- ##
==========================================
- Coverage 68.27% 67.91% -0.37%
==========================================
Files 233 233
Lines 15071 14712 -359
==========================================
- Hits 10290 9991 -299
+ Misses 4401 4363 -38
+ Partials 380 358 -22 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
hwbrzzl
left a comment
There was a problem hiding this comment.
Awesome 👍 Could you add some screenshots in the PR description to show that foreign columns are added as expected?
contracts/database/schema/index.go
Outdated
| Name(name string) IndexDefinition | ||
| } | ||
|
|
||
| type ForeignIdColumnDefinition interface { |
There was a problem hiding this comment.
| type ForeignIdColumnDefinition interface { | |
| type ForeignIDColumnDefinition interface { |
contracts/database/schema/index.go
Outdated
|
|
||
| type ForeignIdColumnDefinition interface { | ||
| driver.ColumnDefinition | ||
| Constrained(table string, column string) ForeignKeyDefinition |
There was a problem hiding this comment.
| Constrained(table string, column string) ForeignKeyDefinition | |
| Constrained(table, column, indexName string) ForeignKeyDefinition |
…ename column into indexName on Constrained()
Done @hwbrzzl |
database/schema/index.go
Outdated
| blueprint *Blueprint | ||
| } | ||
|
|
||
| func (r *ForeignIDColumnDefinition) Constrained(table string, indexName string) schema.ForeignKeyDefinition { |
There was a problem hiding this comment.
| func (r *ForeignIDColumnDefinition) Constrained(table, column, indexName string) schema.ForeignKeyDefinition { | ||
| if column == "" { | ||
| column = "id" | ||
| } | ||
|
|
||
| if table == "" { | ||
| name := r.GetName() | ||
| if strings.HasSuffix(name, "_"+column) { | ||
| base := strings.TrimSuffix(name, "_"+column) | ||
| table = pluralizer.Plural(base) | ||
| } | ||
| } | ||
|
|
||
| return r.References(column, indexName).On(table) | ||
| } | ||
|
|
||
| func (r *ForeignIDColumnDefinition) References(column, indexName string) schema.ForeignKeyDefinition { | ||
| return r.blueprint.Foreign(r.GetName()).References(column).Name(indexName) | ||
| } |
There was a problem hiding this comment.
I would appreciate it if you could add some test cases for these two functions.
fe3f876 to
b6a50fd
Compare
|
Oh, sorry, just one more thing, please run |
…ith constraints and references
b6a50fd to
fed615b
Compare
Done. Kindly review when you have a moment @hwbrzzl |


📑 Description
Closes goravel/goravel#604
Implemented
ForeignID,ForeignUlid, andForeignUuidmethods in theBlueprintclass to facilitate the creation of foreign key unsigned big integer, unsigned ULID, and unsigned UUID columns, respectively, in database migrations.These methods offer a more efficient way to define foreign key columns with appropriate data types or constraints, improving both the flexibility and clarity of migration scripts.
New foreign keys methods:
ForeignID()This method creates an unsigned big integer equivalent column.
ForeignUlid()This method creates a ULID equivalent column.
ForeignUuid()This method creates a UUID equivalent column.
Extended methods for constraints:
Constrained()These new methods support creating foreign key constraints, like
Foreign():With this new
Constrained(), the example above can be written more shorter like this:Evidences:
I ran migrations on
goravel/examplesto test that this feature works as expected. Here's the migration file:This is the query result on the command line.
The foreign key columns created
ForeignID,ForeignUlid, andForeignUuidwill be created as below:Although the created columns are basic columns of unsigned big integer, UUID, and ULID (as the same as Laravel), the columns created with
ConstrainedandReferencesadd constraints as below:Limitation:
The
Constrainedcan't be used without parameters for now because we need to 'magically' set the required parameter with some methods. I'm not familiar with these methods. Further guidance would be helpful to make this happen. As well asForeignIdFor().✅ Checks