From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 8D32A4429A8 for ; Mon, 21 Oct 2019 11:38:58 +0300 (MSK) References: <20191017192009.GA19182@atlas> From: Kirill Shcherbatov Message-ID: <871a2f2d-aae5-871c-1b19-217bb5a6bab3@tarantool.org> Date: Mon, 21 Oct 2019 11:38:57 +0300 MIME-Version: 1.0 In-Reply-To: <20191017192009.GA19182@atlas> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [Tarantool-patches] [tarantool-patches] Re: [PATCH v1 1/1] sql: fix fk violation for autoincremented field List-Id: Tarantool development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Konstantin Osipov , tarantool-patches@freelists.org, tarantool-patches@dev.tarantool.org, korablev@tarantool.org On 17.10.2019 22:20, Konstantin Osipov wrote: > * Kirill Shcherbatov [19/10/17 17:36]: >> Fk constraints used to ignore incremented fields that are >> represented with NULL placeholders in a tuple during FK tests in >> the SQL frontend. A factual value is inserted in a tuple a bit >> later, during DML 'insert' operation on the server side. >> >> To work around this issue a new OP_NextSequenceValue opcode is >> introduced. This new operation retrieves a next sequence value >> (that will be factually inserted in a tuple later) to test it >> for fk constraint compatibility. > > If you added a method to query the next sequence value, why don't > you set it in SQL? This way you don't have to query it, you > increment > and use it in SQL right away. 1. "why don't you set it in SQL" I've spend much time to deal with this concept and now I think that is is not really good idea also. Consider the following example: box.execute("CREATE TABLE t (i INT PRIMARY KEY AUTOINCREMENT, a INT check (a > 0));") box.execute("INSERT OR IGNORE INTO t VALUES (null, 1), (null, -1), (null, 2);") - autoincrement_ids: - 1 - 3 row_count: 2 As you see, the generated ids are attached to the response only when the operation is done. 2. Dealing with problem as proposed, if autoincremented ids were generated beforehand, i.e. (null, 1), (null, -1), (null, 2) -> (1, 1), (2, -1), (3, 2) we wouldn't attach them to the response during generation, because (for some reason, like ck constraint) some parts of the DML request could be declined. At the same time in OP_IdxInsert (in this case) we have no information about the id is generated for the last tuple processed. (in the current Tarantool's architecture it is possible, because the generation is guaranteed to be performed during the tuple insertion - so we take a look for the sequence state). 3. Unfortunately, my previously proposed approach is not correct also: box.execute("CREATE TABLE t1 (s1 INTEGER PRIMARY KEY);") box.execute("CREATE TABLE t2 (s1 INTEGER PRIMARY KEY AUTOINCREMENT, FOREIGN KEY (s1) REFERENCES t1);") box.execute("INSERT INTO t2 VALUES (NULL), (NULL), (NULL);") Looking for the next value of the sequence without advancing the sequence state is not correct, as you could imagine. 4. You know, a completely correct solution for the problem of the ticket #4565 is evaluating fk constraints on the server side :(