[Python] How to rename an existing table?

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create 200+ tables using declarative base on the fly. I learnt it's not possible, so my idea was to create a common table and rename it 200+ times.

    class Movie(Base):

    id = Column(Integer, primary_key=True)
    title = Column(String)
    release_date = Column(Date)
    name=Column(String)
    __tablename__ = 'titanic'

    def __init__(self, newname,title, release_date):
    self.title = title
    self.release_date = release_date


    What is the code to change the table name from "titanic" to "wild"?

    In Postgresql it is

    ALTER TABLE table_name
    RENAME TO new_table_name;


    I am not finding a solution in sqlalchemy.

    There are no foreign keys to this table.

    The objective of this question is to rename an existing table thru a solution (if) available in sqlalchemy, not in a purely python way (as mentioned in the other question).

    Continue reading...

Compartilhe esta Página