postgresql 无法在只读事务中执行 INSERT - Laravel 读/写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24499839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot execute INSERT in a read-only transaction - Laravel read/write
提问by Rodri_gore
I'm trying to set a read/write connection in Laravel 4.2.6 and I cannot perform INSERT, but I can do UPDATE and DELETE transactions. The message that I get is:
我正在尝试在 Laravel 4.2.6 中设置读/写连接,但我无法执行 INSERT,但我可以执行 UPDATE 和 DELETE 事务。我得到的消息是:
SQLSTATE[25006]: Read only sql transaction: 7 ERROR: cannot execute INSERT in a read-only transaction
The configuration of the database is:
数据库的配置是:
'connections' => [
'pgsql' => [
'read' => [
'host' => '192.168.22.10'
],
'write' => [
'host' => '192.168.33.10'
],
'driver' => 'pgsql',
'database' => 'simpo',
'username' => 'postgres',
'password' => 'xxx',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
]
I'm using Postgres 9.3 with 2 server (Virtual machines). The replication works fine and is a simple binary or "Hot Standby" replication (Master/Slave), where the stand by machine can only make read operation.
我正在使用带有 2 个服务器(虚拟机)的 Postgres 9.3。复制工作正常,是一个简单的二进制或“热备”复制(主/从),其中备用机器只能进行读取操作。
EDIT:
编辑:
I tested using query builder and the INSERT works fine, but with Eloquent fails.
我使用查询构建器进行了测试,INSERT 工作正常,但 Eloquent 失败了。
I'm thinking that maybe is a BUG with the connection in the Eloquent model but the unusual thing is that the only INSERT operation fails and not the UPDATE OR DELETE, so that make me think that is some operation that only happen when a INSERT is perform (Maybe with the ID that is generated?)
我在想这可能是 Eloquent 模型中连接的一个 BUG,但不寻常的是唯一的 INSERT 操作失败了,而不是 UPDATE OR DELETE,所以这让我觉得这是一些只有在 INSERT 时才会发生的操作执行(也许使用生成的 ID?)
EDIT 2:
编辑2:
I finally noticed the problem. The error happens when Laravel try to insert and fetch the Id of the row. The problem happen because the framework call a 'select' method but this select execute and "INSERT ... returning 'ID'", so if the machine can only execute SELECT transaction fails because this SELECT come with a INSERT.
我终于注意到了这个问题。当 Laravel 尝试插入和获取行的 Id 时会发生错误。问题发生是因为框架调用了一个'select'方法,但是这个select执行并且“INSERT ...返回'ID'”,所以如果机器只能执行SELECT事务失败,因为这个SELECT带有一个INSERT。
I make a pull request where I check if the select method start with a 'insert' operation, and depending of that get the correct connection https://github.com/laravel/framework/pull/4914
我提出了一个拉取请求,我检查选择方法是否以“插入”操作开始,并根据它获得正确的连接 https://github.com/laravel/framework/pull/4914