Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不同数据库后端的peewee数据库运行时初始化_Python_Orm_Peewee - Fatal编程技术网

Python 不同数据库后端的peewee数据库运行时初始化

Python 不同数据库后端的peewee数据库运行时初始化,python,orm,peewee,Python,Orm,Peewee,我试图编写一个与数据库无关的模块。我想定义我的peewee模型,然后在运行时使用数据库对象的init方法连接到数据库 当我传递一个Sqlite连接字符串时,它会按预期工作,例如 >>> app.db.init('sqlite://mydb.sqlite') 连接到数据库,一切正常。但是当我尝试使用postgres连接字符串时,我得到了一个错误 >>> app.db.init('postgresql://username:password@localhost/

我试图编写一个与数据库无关的模块。我想定义我的peewee模型,然后在运行时使用数据库对象的init方法连接到数据库

当我传递一个Sqlite连接字符串时,它会按预期工作,例如

>>> app.db.init('sqlite://mydb.sqlite')
连接到数据库,一切正常。但是当我尝试使用postgres连接字符串时,我得到了一个错误

>>> app.db.init('postgresql://username:password@localhost/mydb')
...
peewee.OperationalError: FATAL:  database "postgresql://username:password@localhost/mydb" does not exist 
如果我使用单独的参数,我可以得到连接的init方法

>>> app.db.init('mydb', username='username', password='password')
但这在不同的数据库后端之间不能很好地转换


有人能告诉我让init使用连接uri的正确方向吗

我想我已经想出了一个办法。它有点笨重,所以如果有人有更好的建议,我会洗耳恭听。我正在创建一个connect对象,然后将其属性传递给db.init。像这样

>>> from playhouse.db_url import connect
>>> import models
>>> db = connect('postgresql://username:password@localhost/mydb')
>>> models.db.init(db.database, **db.connect_kwargs)

这似乎适用于任何有效的Peewee后端。

我想我已经找到了一种方法来做到这一点。它有点笨重,所以如果有人有更好的建议,我会洗耳恭听。我正在创建一个connect对象,然后将其属性传递给db.init。像这样

>>> from playhouse.db_url import connect
>>> import models
>>> db = connect('postgresql://username:password@localhost/mydb')
>>> models.db.init(db.database, **db.connect_kwargs)

这似乎适用于任何有效的Peewee后端。

在与Peewee作者进行一些交互之后,答案似乎是使用代理对象-

在我的模型模块中,我做到了

database_proxy = pw.Proxy()
...
class Blah(pw.Model):
    column_one = pw.CharField()

    class Meta:
        database = database_proxy
然后在运行时连接我所做的

>>> from playhouse.db_url import connect
>>> import models
>>> db = connect('postgresql://username:password@localhost/mydb')
>>> models.database_proxy.initialize(db)

然后我可以正常地与模型对象交互。通过这种方式,我可以在不同的数据库后端之间切换,只需在我的应用程序配置中设置一个连接URL字符串即可在它们之间切换。

在与Peewee作者进行一些交互之后,答案似乎是使用代理对象-

在我的模型模块中,我做到了

database_proxy = pw.Proxy()
...
class Blah(pw.Model):
    column_one = pw.CharField()

    class Meta:
        database = database_proxy
然后在运行时连接我所做的

>>> from playhouse.db_url import connect
>>> import models
>>> db = connect('postgresql://username:password@localhost/mydb')
>>> models.database_proxy.initialize(db)

然后我可以正常地与模型对象交互。通过这种方式,我可以在不同的数据库后端之间切换,只需在我的应用程序配置中有一个连接URL字符串就可以在它们之间切换。

进一步阅读文档后,似乎Sqlite方法可以工作,因为SqliteDatabase init的第一个参数是数据库文件名,而我的第一个示例没有使用数据库URI。但是文档,特别是这里——和这里——表明我可以使用playhouse.db_url.connect函数返回数据库连接,然后将其传递给db.init。但这也会引发一个错误。进一步阅读文档后,Sqlite方法似乎可以工作,因为SqliteDatabase init的第一个参数是数据库文件名,而我的第一个示例没有使用数据库URI。但是文档,特别是这里——和这里——表明我可以使用playhouse.db_url.connect函数返回数据库连接,然后将其传递给db.init。但这也会引发一个错误,只不过它不适用于Sqlite。如果我将上面示例中的连接字符串更改为sqlite:///Users/andy47/temp_db.sqlite 所有语句都执行。只要我尝试查询任何模型,就会收到一条错误消息;操作错误:致命:数据库用户/andy47/temp_db.sqlite不存在,除非它不能与sqlite一起工作。如果我将上面示例中的连接字符串更改为sqlite:///Users/andy47/temp_db.sqlite 所有语句都执行。只要我尝试查询任何模型,就会收到一条错误消息;操作错误:致命:数据库用户/andy47/temp_db.sqlite不存在