On Tue, Aug 21, 2012 at 5:25 PM, andrea crotti
<andrea.crott...@gmail.com> wrote:
> The question is probably very simple, but I can't find an answer anywhere...
> Suppose I already have some tables declarad in a declarative way, as
> below, how do I create the database schema from them?
>
> I usually always did with the
> meta.create_all() after defining the various "Table('name', meta...)"
>
> in this way it should be even easier but I can't find the magic instruction..
>
>
> class TestStatus(Base):
>     __tablename__ = 'TestStatus'
>
>     id = Column(String, primary_key=True)
>     done = Column(Boolean, default=False)
>
>     def __init__(self, testid):
>         self.testid = testid
>
>
> class AreaTests(Base):
>     __tablename__ = 'AreaTests'
>
>     area = Column(String, primary_key=True)
>     test = Column(String, ForeignKey('TestStatus.id'))
>
>     def __init__(self, area, test):
>         self.area = area
>         self.test = test
>
>
> class Results:
>     """Keep track of the results of the test that have been run, using
>     an temporary sqlite database to store
>     """
>     def __init__(self):
>         db_path = 'sqlite:///%s' % SQLITE_TEMP
>         eng = create_engine(db_path)
>         meta = MetaData(bind=eng)
>         # must first create the schema if not present already
>         self.session = sessionmaker(bind=eng)()
>
>     def add_area(self, area, testid):
>         """Add an area and a testid to this simple database
>         """
>         test_obj = TestStatus(testid)
>         if test_obj not in self.session:
>             self.session.add(test_obj)
>
>         self.session.add(AreaTests(area, testid))
>         self.session.commit()
>

The MetaData instance is available via the declarative base class, so
you should be able to do something like:

  Base.metadata.create_all()

http://docs.sqlalchemy.org/en/rel_0_7/orm/extensions/declarative.html#accessing-the-metadata

Hope that helps,

Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to