2

How can I create a Lumen migration with a column that references a table that has an unrelated name to the column name?

Example:

The following would throw an error that user_destinations can't be found.

$table->foreign('user_destination')->references('id')->on('locations');

or

$table->foreignId('warehouse_isle_shelf_id')->constrained();

The intention here is for it to look for warehouse_isles instead of warehouse_isle_shelves or warehouse_isle_shelfs as I'm not sure how Lumen handles plurals for words who's plurals aren't just taking the singular form and appending an s.

njk18
  • 153
  • 7
  • Although Lumen is a micro-framework, so the way you write the migrations is one and the same, I believe. – gowl Feb 18 '22 at 13:46

1 Answers1

2

Your code here should work because you have referenced the table name locations:

$table->foreign('user_destination')->references('id')->on('locations');

But the second line is asking Laravel to guess the name. Here's the relevant part talking about it in the documentation:

The foreignId method is an alias for unsignedBigInteger while the constrained method will use convention to determine the table and column name being referenced. If your table name does not match the convention, you may specify the table name by passing it as an argument to the constrained method:

$table->foreignId('user_id')->constrained('users');
gowl
  • 314
  • 1
  • 11
  • 1
    Thank you for noticing, however, the code examples are just that, examples to a more general question. My question is about naming a column that references another column without that first column's name having any restrictions (such as being the singular form of the referenced table). – njk18 Feb 18 '22 at 13:34
  • Ah, silly me. Thought it was that simple. I have modified the answer to give you a proper answer. Hope it works. – gowl Feb 18 '22 at 14:01