pgmodeler icon indicating copy to clipboard operation
pgmodeler copied to clipboard

Allow renaming auto-generated foreign key column names when creating a connection

Open Nezteb opened this issue 2 years ago • 12 comments

Feature description When I connect two tables with a one-to-many relationship, the foreign key name on the recevier is automatically generated:

Screenshot 2023-06-08 at 12 54 57 PM

In the above screenshot, I want to be able to name the foreign key to user_id, not id_users.

I tried changing the settings for this connection, but nothing I try seems to do what I'd like:

Screenshot 2023-06-08 at 12 57 36 PM

If you try to edit the column directly, you can't because it is protected:

Screenshot 2023-06-08 at 1 05 23 PM

Nezteb avatar Jun 08 '23 20:06 Nezteb

I am in the same boat. I have a table that has a relationship to itself (think employee->manager relationship), but the created foreign key is just called id_employee and I can't change the name to id_manager.

If anybody has a workaround please let me know.

dekiesel avatar Jan 29 '24 14:01 dekiesel

Does changing the name patterns doesn't work?

rkhaotix avatar Jan 29 '24 15:01 rkhaotix

@rkhaotix I am not sure if you meant me, but: I am trying to change the name itself, not the pattern.

dekiesel avatar Jan 29 '24 15:01 dekiesel

@dekiesel Hi, yes. :)

Since the generated objects' names are ruled by the patterns you have to alter the patterns themselves to get the desired results.

In your case, you can hard-code "id_employee" as the column pattern.

rkhaotix avatar Jan 29 '24 15:01 rkhaotix

@dekiesel Hi, yes. :)

Since the generated objects' names are ruled by the patterns you have to alter the patterns themselves to get the desired results.

In your case, you can hard-code "id_employee" as the column pattern.

Ah, thanks! :)

In the meantime I found that I can "convert" the relationship and with that I was able to rename the column too. Is that effectively the same or should I not have done that?

dekiesel avatar Jan 29 '24 15:01 dekiesel

@dekiesel The convert feature is an alternative way to do what you want. Anyway, there are some drawbacks by doing that. See here:

https://www.pgmodeler.io/support/docs/718-managing-relationship-generated-objects?v=1.1.0

rkhaotix avatar Jan 29 '24 16:01 rkhaotix

That was an interesting read, thank you very much!!

dekiesel avatar Jan 30 '24 07:01 dekiesel

Hi, I am having an issue which looks correlated. Not sure if it's worth a dedicated thread. I have generated a table with a very large name (57 characters, below the limit of 63). When I generate a relationship with another table, the automatically generated foreign key column name looks like this:

image

Even if the automatically generated name with the pattern {sc}{st} should be id{table_name} = 60 chars < 63.

I also tried to manually change the pattern to:

image

And I get the same issue. I then tried to convert the relationship

image

And then, again by manually modifying the pattern to the desired name, I was able to get the desired result:

image

So this does not seem to be due to the maximum number of 63 characters (or not only, at least, since this issue does not happen with significantly shorter names). Is this behaviour expected or is it a bug? I know I could work this around by simply user shorter names but I am importing an old model that worked with that name and now changing the tables' names would have a non-negligible impact.

Thanks!

MarcelloSciarra avatar Apr 04 '24 10:04 MarcelloSciarra

Hi @MarcelloSciarra

Unfortunately, this is a limitation of the tool. :(

pgModeler will automatically truncate object names that exceed 63 bytes. But it's not a simple truncation, it'll append six characters for disambiguation purposes.

Let me explain, say we're creating two tables whose names exceed the name length limits. For this example, let's say that the maximum allowed length for names is 20, so our tables will be named a_table_with_long_name_AAA and a_table_with_long_name_BBB.

Before adding the tables in the model, pgModeler does the following:

  1. Truncates the names to the maximum allowed length:
  • a_table_with_long_name_AAA is renamed to a_table_with_long_na
  • a_table_with_long_name_BBB is also renamed to a_table_with_long_na
    See how their names are the same after the truncation?
  1. Here is where the disambiguation procedure is applied. To avoid name conflicts, pgModeler removes the last 6 characters from the truncated names and appends 6 new characters coming from the md5 hash of the objects' original names, so, the new names will be:
  • a_table_with_long_name_AAA -> a_table_with__099f4c
  • a_table_with_long_name_BBB -> a_table_with__d4489e
    If we don't do this step, pgModeler will add the first table without problems, but when adding the second, it'll raise an error related to name conflicts.

Of course, this isn't the best solution because the disambiguation may fail in cases where the involved hashes have the same 6 first characters. But this way, we diminish the chance of name conflicts.

The solution you already deduced: for now, is to use shorter names. That's until I find a better solution.

rkhaotix avatar Apr 04 '24 21:04 rkhaotix

Hi @rkhaotix,

Thanks for the detailed answer! I guess the only thing that is not fully clear to me is why the name is truncated in the first place if it does not exceed the 63 characters. And, again, this only happens when the name is auto-generated but it does not happen if I "convert" the relationship and define the exact same name manually, which is below 63 chars and it works just fine.

Any hint on that? Is the truncation performed if (length+6) > 63 when the name is automatically generated?

Thanks!

MarcelloSciarra avatar Apr 05 '24 07:04 MarcelloSciarra

Hi @rkhaotix,

Thanks for the detailed answer! I guess the only thing that is not fully clear to me is why the name is truncated in the first place if it does not exceed the 63 characters. And, again, this only happens when the name is auto-generated but it does not happen if I "convert" the relationship and define the exact same name manually, which is below 63 chars and it works just fine.

Any hint on that? Is the truncation performed if (length+6) > 63 when the name is automatically generated?

Hi @MarcelloSciarra

You are right! The disambiguation routine is not working well for long names. I'll patch it! ;)

rkhaotix avatar Apr 05 '24 14:04 rkhaotix

Hi @rkhaotix,

great, thanks a lot! :)

MarcelloSciarra avatar Apr 08 '24 11:04 MarcelloSciarra