Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a flask application which I'm trying to convert into Django. In one of the models which inherit an abstract base model, it is mentioned as

__table_args__ = {'extend_existing': True}

Can someone please explain what this means in SQLAlchemy with a small example.

I have gone through few articles but as I worked on Django and new to Flask-SQLAlchemy I couldn't understand it properly.

https://hackersandslackers.com/manage-database-models-with-flask-sqlalchemy/ https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/table_config.html

like image 821
Underoos Avatar asked Dec 08 '25 05:12

Underoos


1 Answers

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.

like image 122
Jinghui Niu Avatar answered Dec 10 '25 17:12

Jinghui Niu



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!