小编blu*_*onk的帖子

使用 SQLAlchemy 反映 postgres 数据库中的每个模式

我有一个现有的数据库,它有两个模式,命名为schoolsstudents,包含在一个实例中,declarative_base并通过从该实例继承的两个不同类

class DirectorioEstablecimiento(Base):
    __table_args__ = {'schema': 'schools'}
    __tablename__ = 'addresses'
    # some Columns are defined here
Run Code Online (Sandbox Code Playgroud)

class Matricula(Base):
    __table_args__ = {'schema': 'students'}
    __tablename__ = 'enrollments'
    # some Columns are defined here
Run Code Online (Sandbox Code Playgroud)

我可以使用该Base实例Base.metadata.create_all(bind=engine)在我在 postgres 中拥有的测试数据库中重新创建它。如果我查询,我可以确认这是没有问题的pg_namespace

In [111]: engine.execute("SELECT * FROM pg_namespace").fetchall()
2017-12-13 18:04:01,006 INFO sqlalchemy.engine.base.Engine SELECT * FROM pg_namespace
2017-12-13 18:04:01,006 INFO sqlalchemy.engine.base.Engine {}

Out[111]: 
[('pg_toast', 10, None),
 ('pg_temp_1', 10, None),
 ('pg_toast_temp_1', 10, None),
 ('pg_catalog', 10, '{postgres=UC/postgres,=U/postgres}'),
 ('public', 10, …
Run Code Online (Sandbox Code Playgroud)

python postgresql sqlalchemy database-schema

6
推荐指数
1
解决办法
1693
查看次数

`shutil.rmtree`对`tempfile.TemporaryDirectory()`不起作用

考虑这个测试

import shutil, tempfile
from os import path
import unittest

from pathlib import Path

class TestExample(unittest.TestCase):
    def setUp(self):
        # Create a temporary directory
        self.test_dir = tempfile.TemporaryDirectory()
        self.test_dir2 = tempfile.mkdtemp()

    def tearDown(self):
        # Remove the directory after the  test
        shutil.rmtree(self.test_dir2) 
        shutil.rmtree(self.test_dir.name) #throws error

    def test_something(self):
        self.assertTrue(Path(self.test_dir.name).is_dir())
        self.assertTrue(Path(self.test_dir2).is_dir())

if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

tearDown但引发错误

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpxz7ts7a7'
Run Code Online (Sandbox Code Playgroud)

指的是self.test_dir.name.

根据源代码tempfile,两个元素是相同的.

    def __init__(self, suffix=None, prefix=None, dir=None):
        self.name = mkdtemp(suffix, prefix, dir) …
Run Code Online (Sandbox Code Playgroud)

python shutil temporary-files python-3.x python-unittest

3
推荐指数
2
解决办法
410
查看次数