Martin Reguly
Martin Reguly

Reputation: 143

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?

Upvotes: 3

Views: 981

Answers (1)

Ilja Everilä
Ilja Everilä

Reputation: 53017

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')))
                """
            }
        }
    )

Upvotes: 4

Related Questions