Dynamic Table Creation and ORM mapping in SqlAlchemy

前端 未结 7 1853
忘了有多久
忘了有多久 2020-12-23 19:18

I\'m fairly new to using relational databases, so I prefer using a good ORM to simplify things. I spent time evaluating different Python ORMs and I think SQLAlchemy is what

7条回答
  •  遥遥无期
    2020-12-23 19:54

    you can use declarative method for dynamically creating tables in database

    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
    
    
    Base = declarative_base()
    
    class Language(Base):
        __tablename__ = 'languages'
    
        id = Column(Integer, primary_key=True)
        name = Column(String(20))
        extension = Column(String(20))
    
        def __init__(self, name, extension):
            self.name = name
            self.extension = extension
    

提交回复
热议问题