many-to-many nullable columns with through by nikelborm · Pull Request #852 · ormar-orm/ormar
such code:
... 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) createdByUser: Optional[User] = ormar.ForeignKey(User, name="created_by_user_id", nullable=True, unique=False) isPublic: bool = ormar.Boolean( name="is_public", nullable=False, default=False) name: str = ormar.Text( name="name", nullable=False) description: str = ormar.Text( name="description", nullable=True) tickers: Optional[List[Ticker]] = ormar.ManyToMany( to=Ticker, through=TickerToTickerCollection, related_name="tickerCollections", through_relation_name="ticker_collection_id", through_reverse_relation_name="ticker_id", # is_through_relation_column_nullable=False, # is_through_reverse_relation_column_nullable=False, ) ...
cause such migration:
... op.create_table('ticker_to_ticker_collection', sa.Column('id', sa.Integer(), nullable=False), sa.Column('ticker_id', sa.Integer(), nullable=True), sa.Column('ticker_collection_id', sa.Integer(), nullable=True), sa.ForeignKeyConstraint(['ticker_collection_id'], ['ticker_collection.ticker_collection_id'], name='asd1', onupdate='CASCADE', ondelete='CASCADE'), sa.ForeignKeyConstraint(['ticker_id'], ['ticker.ticker_id'], name='asd2', onupdate='CASCADE', ondelete='CASCADE'), sa.PrimaryKeyConstraint('id') ) ...
Here ticker_id and ticker_collection_id are nullable by default, and there is no built-in way to set them non nullable.
I tried to set sql_nullable and nullable in arguments of ormar.ManyToMany but looks like it affects only typing information.
So then I added these two parameters, that will allow to override nullability of both through columns.