-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
Added fk_name parameter to ormar.ForeignKey #849
base: master
Are you sure you want to change the base?
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_name
parameter toormar.ForeignKey
and it allows me to specify foreign constraint name like this:
and it creates migration which executes successfully: