def declarative_base(dependencies=None): result = sa_declarative_base( cls=BaseClass, constructor=None, class_registry=_CLASS_REGISTRY, ) dependencies = () if dependencies is None else dependencies result.metadata.dependencies = dependencies return result
def test_functional_metadata(self): Base = sa_declarative_base() class Model(Base): __tablename__ = 'model' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) register_session(transactional=False, metadata=Base.metadata) session = get_session() session.add(Model(name='foo')) session.commit()
def test_functional(self): Base = sa_declarative_base() class Model(Base): __tablename__ = 'model' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) register_session( transactional=False, config=Config(Source(Model.__table__))) session = get_session() session.add(Model(name='foo')) session.commit()
def test_tricky_to_delete(self): # respect any DB_URL set here so that # we sure the real db here to make sure # delete works across all our DB types... db_path = ( os.environ.get('DB_URL', '').strip() or 'sqlite:///'+os.path.join(self.dir.path, 'test.db') ) Base = sa_declarative_base() class Model1(Base): __tablename__ = 'model1' id = Column(Integer, primary_key=True) model2_id = Column(Integer, ForeignKey('model2.id')) model2 = relationship("Model2") class Model2(Base): __tablename__ = 'model2' id = Column('id', Integer, primary_key=True) # create in one session register_session(db_path, name='create', transactional=False, metadata=Base.metadata) m1 = Model1() m2 = Model2() m1.model2 = m2 session = get_session('create') if db_path.startswith('sqlite:'): session.execute('PRAGMA foreign_keys = ON') session.add(m1) session.add(m2) session.commit() compare(session.query(Model1).count(), 1) compare(session.query(Model2).count(), 1) session.rollback() # now register another session which should # blow the above away register_session(db_path,name='read', transactional=False, metadata=Base.metadata) session = get_session('read') compare(session.query(Model1).count(), 0) compare(session.query(Model2).count(), 0) session.rollback()
def test_tricky_to_delete(self): # respect any DB_URL set here so that # we sure the real db here to make sure # delete works across all our DB types... db_path = (os.environ.get('DB_URL', '').strip() or 'sqlite:///' + os.path.join(self.dir.path, 'test.db')) Base = sa_declarative_base() class Model1(Base): __tablename__ = 'model1' id = Column(Integer, primary_key=True) model2_id = Column(Integer, ForeignKey('model2.id')) model2 = relationship("Model2") class Model2(Base): __tablename__ = 'model2' id = Column('id', Integer, primary_key=True) # create in one session register_session(db_path, name='create', transactional=False, metadata=Base.metadata) m1 = Model1() m2 = Model2() m1.model2 = m2 session = get_session('create') if db_path.startswith('sqlite:'): session.execute('PRAGMA foreign_keys = ON') session.add(m1) session.add(m2) session.commit() compare(session.query(Model1).count(), 1) compare(session.query(Model2).count(), 1) session.rollback() # now register another session which should # blow the above away register_session(db_path, name='read', transactional=False, metadata=Base.metadata) session = get_session('read') compare(session.query(Model1).count(), 0) compare(session.query(Model2).count(), 0) session.rollback()
def test_only_some_packages(self): Base = sa_declarative_base() class Model1(Base): __tablename__ = 'model1' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) class Model2(Base): __tablename__ = 'model2' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) register_session(transactional=False, config=Config(Source(Model1.__table__))) # only table1 should have been created! compare([u'model1'], Inspector.from_engine(get_session().bind).get_table_names())
def declarative_base(**kw): """ Return a :obj:`Base` as would be returned by :func:`~sqlalchemy.ext.declarative.declarative_base`. Only one :obj:`Base` will exist for each combination of parameters that this function is called with. If it is called with the same combination of parameters more than once, subsequent calls will return the existing :obj:`Base`. This method should be used so that even if more than one package used by a project defines models, they will all end up in the same :class:`~sqlalchemy.schema.MetaData` instance and all have the same declarative registry. """ key = tuple(kw.items()) if key in _bases: return _bases[key] base = sa_declarative_base(**kw) _bases[key] = base return base
def test_only_some_packages(self): Base = sa_declarative_base() class Model1(Base): __tablename__ = 'model1' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) class Model2(Base): __tablename__ = 'model2' id = Column('id', Integer, primary_key=True) name = Column('name', String(50)) register_session( transactional=False, config=Config(Source(Model1.__table__))) # only table1 should have been created! compare( [u'model1'], Inspector.from_engine(get_session().bind).get_table_names() )
def __call__(self): if not self._instance: self._instance = sa_declarative_base() return self._instance
def declarative_base(): return sa_declarative_base( cls=BaseClass, constructor=None, class_registry=_CLASS_REGISTRY, )