Is there a last_insert_id method for Sequel?

After insertion, I need to know the identifier from the string, how can I do this?

+3
source share
2 answers

According to the Dataset # insert documentation , the return value for is insert()usually the primary key of the inserted row, but it depends on which adapter you use.

+2
source

I tried the same with SQL server and unique identifier, but to no avail. Unique identifier is not returned.

Extract my definition:

CREATE TABLE [dbo].[MyTable](
     [ID] [uniqueidentifier] NOT NULL,
     [Data] [nvarchar](255) NULL
)
ALTER TABLE [dbo].[MyTable] ADD  DEFAULT (newid()) FOR [ID]

When I insert with Sequel:

DB[:MyTable].insert( :Data => 'data' )

the dataset is added with a unique identifier, but the return code if Dataset # insert is nil.

WITH

DB[:MyTable].insert(:ID => Sequel.function(:newid),  :Data => 'data' )

.

key = Sequel.function(:ID)
DB[:MyTable].insert(:ID => key,  :Data => 'data' )

"" - , . Sequel.function(:ID).f " ". "- error

, :

class MyTable < Sequel::Model(:MyTable); end 
entry = MyTable.create(:Data => 'data')
$uniqueidentifier = entry[:ID]
+1

Source: https://habr.com/ru/post/1741855/


All Articles