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

Table name set via meta is not honoured by foreign keys #684

Open
Orteko opened this issue Apr 17, 2024 · 1 comment
Open

Table name set via meta is not honoured by foreign keys #684

Orteko opened this issue Apr 17, 2024 · 1 comment
Labels
bug Something isn't working pending This issue is pending review

Comments

@Orteko
Copy link

Orteko commented Apr 17, 2024

  • Laravel Version: 11.3.1
  • PHP Version: 8.3.6
  • Blueprint Version: 2.10.0
  • Platform: Linux

Issue:

First up - thanks for the project, wish i'd found it earlier as it's going to save me a lot of grunt work!

On to the issue - as per #577 & #639 we appear to have the ability to use set a custom table name via meta when defining a model.

This works and $table is added to the model with the expected value.

However, when attempting to create a second model (Service in the included example) that references a model which has a custom table name defined, that custom table name is not used when creating the foreign key.

With the draft.yaml in this report - when running migrations a ERROR: relation "customers" does not exist (Connection: pgsql, SQL: alter table "service" add constraint "service_customer_id_foreign" foreign key ("customer_id") references "customers" ("id")) exception will be produced.

The culprit appears to be that Generators/MigrationGenerator.php @ line 318 - updating this line to include the table in the constrained() method "fixes" the issue, and both the models with custom table names and those without appear to migrate as expected.

However it then would operate identically to the if statement below it so i'm uncertain as to the underlying reason for these two if statements to be different:

diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php
index b8c63a5..e105c5f 100644
--- a/src/Generators/MigrationGenerator.php
+++ b/src/Generators/MigrationGenerator.php
@@ -315,7 +315,7 @@ class MigrationGenerator extends AbstractClassGenerator implements Generator
                 $on_update_suffix = '->cascadeOnUpdate()';
             }
             if ($column_name === Str::singular($table) . '_' . $column) {
-                return self::INDENT . "{$prefix}->constrained(){$on_delete_suffix}{$on_update_suffix}";
+                return self::INDENT . "{$prefix}->constrained('{$table}'){$on_delete_suffix}{$on_update_suffix}";
             }
             if ($column === 'id') {
                 return self::INDENT . "{$prefix}->constrained('{$table}'){$on_delete_suffix}{$on_update_suffix}";

The 'meta' functionality is also not documented on the website as far as I can see so i'll open a separate issue at https://github.com/laravel-shift/blueprint-docs.git assuming it is expected to be supported.

draft.yaml:

models:
  Customer:
    name: unique string index
    meta:
      table: customer
  Service:
    customer_id: id foreign:customer.id
    name: unique string index
  Plan:
    name: unique string index
  Rate:
    plan_id: id foreign:plans.id
    name: unique string index

controllers:
  Customer:
    resource
  Service:
    resource
  Plan:
    resource
  Rate:
    resource
@Orteko Orteko added bug Something isn't working pending This issue is pending review labels Apr 17, 2024
@jasonmccreary
Copy link
Collaborator

Yeah, that's getting deep. I could see fixing this for a single build run, but I wonder about future runs. Blueprint would either need to "remember" this custom table name or you would need to specify it, always. I'll have to think on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending This issue is pending review
Projects
None yet
Development

No branches or pull requests

2 participants