Added fk_name parameter to ormar.ForeignKey#849
Added fk_name parameter to ormar.ForeignKey#849nikelborm wants to merge 4 commits intoormar-orm:masterfrom
Conversation
It will allow users to override automatically generated foreign key constraint name (for example in case when generated constraint name is too long for some DB)
|
If I need to extend documentation, please point me where I can edit it. |
|
Added fk_name to many-to-many because it has the same problem with too long names ...
class Ticker(ormar.Model):
class Meta(BaseMeta):
tablename = "ticker"
id: int = ormar.Integer(name="ticker_id", nullable=False, primary_key=True, autoincrement=True)
code: str = ormar.String(name="code", nullable=False, unique=True, max_length=10)
class TickerToTickerCollection(ormar.Model):
class Meta(BaseMeta):
tablename = "ticker_to_ticker_collection"
class TickerCollection(ormar.Model):
class Meta(BaseMeta):
tablename = "ticker_collection"
id: int = ormar.Integer(name="ticker_collection_id", nullable=False, primary_key=True, autoincrement=True)
tickers: Optional[List[Ticker]] = ormar.ManyToMany(
to=Ticker,
through=TickerToTickerCollection,
related_name="tickerCollections",
# through_relation_fk_name="asd1",
# through_reverse_relation_fk_name="asd2",
through_relation_name="ticker_collection_id",
through_reverse_relation_name="ticker_id",
)
...previous code causes migration to fail with the same "too long constraint name" error but if I uncomment ...
through_relation_fk_name="asd1",
through_reverse_relation_fk_name="asd2",
...asd1 and asd2 will override constraint names in generated ticker_to_ticker_collection table in migration |
|
There are missing tests. |
|
do it end? current I got hit by this issue |
|
@Stvchm9703 I dont have time to finish it now. If You want you can add missing tests. I am not very proficient in python, and I wrote even this little part of code with some effort. And I wanted to ask a little help here |
It will allow users to override automatically generated foreign key constraint name (for example in case when generated constraint name is too long for some DB)
In my case this code
when used to generate migration causes such alembic migration:
and when I try to execute it, migration fails with such error:
so I found a way to fix it, by adding
fk_nameparameter toormar.ForeignKeyand it allows me to specify foreign constraint name like this:
and it creates migration which executes successfully: