sqlalchemy orm skip already-existing tables on create

Question:

How can I avoid this exception in sqlalchemy orm when I try to create a table which already exists in a database:

sqlalchemy.exc.InvalidRequestError: Table ‘col1’ is already defined for this MetaData instance. Specify ‘extend_existing=True’ to redefine options and columns on an existing Table object.

Base = automap_base()
Base.prepare(engine, reflect=True)

class Col1(Base):
    __tablename__ = 'col1'
    id = Column(Integer(), primary_key=True)
    name = Column(String())

Base.metadata.create_all(engine)
Asked By: SayPy

||

Answers:

I needed to add {'useexisting': True} (in SQLAlchemy 1.4+, use {'extend_existing': True}).

class Col1(Base):
    __tablename__ = 'col1'
    __table_args__ = {'useexisting': True}
    id = Column(Integer(), primary_key=True)
    name = Column(String())
Answered By: SayPy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.