sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

create_table_from_entity ignores max identifier length

Open cemoktra opened this issue 3 years ago • 8 comments

Description

create_table_from_entity create a foreign key name, that can get quite long. In my case 69 characters. The maximum in MySQL is 64 characters.

Steps to Reproduce

Generate schema for a tables that reference each other and have quite long names. Like 20 characters.

Expected Behavior

Foreign key name might be trimmed to 64 characters. Or add a possibility to specify foreign key names.

cemoktra avatar May 12 '22 11:05 cemoktra

Hey @cemoktra, thanks for the report! You can override name of the foreign key via one of

  • https://github.com/SeaQL/sea-orm/blob/b59f3c58deb04f70476a04263c14f9d21d9596ef/tests/common/features/active_enum_child.rs#L16-L25
  • https://github.com/SeaQL/sea-orm/blob/b59f3c58deb04f70476a04263c14f9d21d9596ef/src/entity/relation.rs#L170-L174

billy1624 avatar May 13 '22 11:05 billy1624

@billy1624 maybe we can make a compiler_error. What do you think?

ikrivosheev avatar May 15 '22 21:05 ikrivosheev

Seems the maximum allowed length of index / foreign key name were 64 characters or no limit depending on the db backend.

  • For MySQL, MariaDB and PostgreSQL, it's 64 characters
  • No such limit for SQLite

I think we should throw a error / warning on SeaQuery side @ikrivosheev Is it possible to throw a compiler error at compile time?

billy1624 avatar May 16 '22 03:05 billy1624

@billy1624 I will try to do this...

ikrivosheev avatar May 16 '22 06:05 ikrivosheev

Thanks! @ikrivosheev

billy1624 avatar May 16 '22 08:05 billy1624

As I mentioned above you can override the generated foreign key name, https://github.com/SeaQL/sea-orm/issues/720#issuecomment-1125967188. And, @ikrivosheev, I don't think we can produce a error / warning for long foreign key name at compile time because the name isn't a constant that we can evaluate at compile time.

billy1624 avatar Jun 16 '22 08:06 billy1624

Actually, I think we can leave it as is. It might be a good idea for the linter, though

tyt2y3 avatar Jun 19 '22 09:06 tyt2y3

  • For MySQL, MariaDB and PostgreSQL, it's 64 characters
  • No such limit for SQLite

Hey everyone, would truncating the name of index/foreign key exceed those limit be a good idea? i.e. implementing the query writer in db dependent manner inside SeaQuery.

billy1624 avatar Aug 05 '22 04:08 billy1624

I don't think truncation is a correct behaviour. And if it becomes a runtime error, I guess not much value than it becoming a SQL error.

tyt2y3 avatar Feb 21 '23 10:02 tyt2y3