gogasca
gogasca

Reputation: 10058

Create table/generate schema in Postgres DB from Model in Python

I'm trying to re-start work for a previous project and Database server was deleted. No database backup or SQL script. But schema is defined in Python Database Model. Can I generate Database schema from it?

I have the following class defined:

class News(db.Model, AutoSerialize, Serializer):
    __tablename__ = 'news'

    news_id = Column(Integer, primary_key=True, server_default=text(
        "nextval('news_news_id_seq'::regclass)"))
    source = Column(String(64))
    source_id = Column(String(64))
    author = Column(String(128))
    title = Column(String(256), nullable=False)
    description = Column(String(65536))
    url = Column(String(512))
    url_to_image = Column(String(512))
    published_at = Column(Date())
    content = Column(String(65536))
    campaign = Column(String(16))
    score = Column(Float(53))
    magnitude = Column(Float(53))
    sentiment = Column(String(16))
    rank_score = Column(Float(53))
    rank_order = Column(Integer)
    translated_content = Column(String(65536))
    detected_language = Column(String(128))
    inserted_at = Column(DateTime(True))

and


t_tags = Table(
    'tags', metadata,
    Column('tag_id', Integer, nullable=False,
           server_default=text("nextval('tags_tag_id_seq'::regclass)")),
    Column('tag_name', String(128), nullable=False)
)

Upvotes: 1

Views: 505

Answers (1)

Petr Blahos
Petr Blahos

Reputation: 2433

When you have got the metadata and connection, you can just do

metadata.create_all(connection)

(I assume db.Model is a declarative base)

Upvotes: 2

Related Questions