[Fixed]-What is the use of __table_args__ = {'extend_existing': True} in SQLAlchemy?

10👍

When set to True, tells that table is already present in the Metadata.
This parameter is to determine wheter this table is allowed to add new columns for its inheriting classes, or to ‘extend’ so to speak.

For example, I have:

class User(Base):
  __table_args__ = {'extend_existing': True}
  
  attr1 = Column(String)
  attr2 = Column(Integer)

class ChildUser(User):
  attr3 = Column(Integer) # this will allow this column to 'extend' the parent attriubtes.

This parameter is default to ‘False’, and normally you wouldn’t want to change this setting.

Leave a comment