Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass positional and keyword arguments in `__table_args__` in SQLAlchemy

Is it possible to use foreign key reference and have oracle partition on same table in sqlalchemy?

Here is how the oracle partition is defined in __table_args__ as a dict

class SQLAlchemyTable(mx.InsertedAtUpdatedAtMixin, Base):
    __tablename__ = 'SQLALCHEMY_TABLE'
    __table_args__ = {
        'info': {
            'oracle_partition': """
                PARTITION BY RANGE (PARTITION_DATE) INTERVAL (NUMTOYMINTERVAL(1,'MONTH'))
                ( PARTITION p_init VALUES LESS THAN (TO_DATE('07-12-2018','DD-MM-YYYY')))
            """
        },
    }

I have found in documentation that ForeignKeyConstraint is defined in __table_args__ but as a tuple not as a dictionary

__table_args__ = (
    ForeignKeyConstraint(('LIST', 'STATE'), ['CODES.LIST_ID', 'CODES.ID']),
)

Any help?

like image 406
Martin Reguly Avatar asked Dec 08 '25 06:12

Martin Reguly


1 Answers

You can pass both positional and keyword arguments in __table_args__, as shown in "Table Configuration". Use a tuple holding the positional arguments, and a dictionary of keyword arguments as the last item of the tuple:

class SQLAlchemyTable(mx.InsertedAtUpdatedAtMixin, Base):
    ...
    __table_args__ = (
        ForeignKeyConstraint(('LIST', 'STATE'), ['CODES.LIST_ID', 'CODES.ID']),
        {
            'info': {
                'oracle_partition': """
                    PARTITION BY RANGE (PARTITION_DATE) INTERVAL (NUMTOYMINTERVAL(1,'MONTH'))
                    ( PARTITION p_init VALUES LESS THAN (TO_DATE('07-12-2018','DD-MM-YYYY')))
                """
            }
        }
    )
like image 133
Ilja Everilä Avatar answered Dec 09 '25 21:12

Ilja Everilä



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!