File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change 7
7
use Exception ;
8
8
use ReflectionClass ;
9
9
use ReflectionMethod ;
10
+ use Tarantool \Client \Exception \RequestFailed ;
10
11
use Tarantool \Client \Keys ;
11
12
use Tarantool \Client \Request \InsertRequest ;
12
13
use Tarantool \Client \Response ;
@@ -115,7 +116,15 @@ public function castIndex(array $fields): ?array
115
116
public function create (array $ data )
116
117
{
117
118
if (!array_key_exists ('id ' , $ data ) && $ this ->fields [0 ] == 'id ' ) {
118
- [$ data ['id ' ]] = $ this ->mapper ->client ->call ("box.sequence. $ this ->name :next " );
119
+ try {
120
+ [$ data ['id ' ]] = $ this ->mapper ->client ->call ("box.sequence. $ this ->name :next " );
121
+ } catch (RequestFailed $ e ) {
122
+ if (str_contains ($ e ->getMessage (), "box.sequence. $ this ->name :next " )) {
123
+ $ this ->mapper ->client ->call ('box.schema.sequence.create ' , $ this ->name );
124
+ return $ this ->create ($ data );
125
+ }
126
+ throw $ e ;
127
+ }
119
128
}
120
129
[$ tuple ] = $ this ->mapper ->client ->getSpaceById ($ this ->id )->insert ($ this ->getTuple ($ data ));
121
130
return $ this ->getInstance ($ tuple );
Original file line number Diff line number Diff line change @@ -123,6 +123,28 @@ public function testCreateRow()
123
123
124
124
$ tester ->drop ();
125
125
126
+ // 'id' field present, but sequence isn't created
127
+ $ tester = $ mapper ->createSpace ('tester ' );
128
+ $ format = [
129
+ [
130
+ 'name ' => 'id ' ,
131
+ 'type ' => 'unsigned '
132
+ ],[
133
+ 'name ' => 'value ' ,
134
+ 'type ' => 'string '
135
+ ]
136
+ ];
137
+ $ tester ->setFormat ($ format );
138
+ $ mapper ->client ->call ("box.space.tester:format " , $ format );
139
+ $ tester ->addIndex (['id ' ]);
140
+ $ testRow = ['value ' => 'apple ' ];
141
+ $ tester ->create ($ testRow );
142
+ $ result = $ mapper ->client ->evaluate ("return box.space.tester:select() " )[0 ][0 ];
143
+ $ this ->assertNotSame ($ result [0 ], 0 );
144
+ $ this ->assertSame ($ testRow ['value ' ], $ result [1 ]);
145
+ $ this ->assertNotNull ($ mapper ->client ->evaluate ('return box.sequence.tester ' )[0 ]);
146
+ $ tester ->drop ();
147
+
126
148
// There is 'id' field and it is first, sequense is created
127
149
$ tester = $ mapper ->createSpace ('tester ' );
128
150
$ tester ->addProperty ('id ' , 'unsigned ' );
You can’t perform that action at this time.
0 commit comments