database interface _unique not working

Post Reply
Lucky24
Posts: 52
Joined: Sun Sep 12, 2010 1:50 am

database interface _unique not working

Post by Lucky24 »

I can't seem to get the _unique value for a sheet to work with the database interface:

Here's my unit test:
Code: [show] | [select all] lua
local params = {
	areas={
		id="",
		name="",
	      _index = { "id" },
	      _unique = { "id" },
	      _violations = "REPLACE"
	},
}
local myval = db:create("map",params)

local myDB = db:get_database("map")
--local deleted = db:delete(myDB.areas,true)
local added = db:add(myDB.areas, {id="1", name="Room#1"})
local fetched = db:fetch(myDB.areas)
display(myDB.areas)
display(fetched)
This outputs:

Values for table: myDB.areas:
(1). '_sht_name', 'areas'.
(1). 'id', table: 09A55080
(2). 'database', 'map'.
(2). 'type', 'string'.
(2). 'name', 'id'.
(2). 'sheet', 'areas'.
(1). 'name', table: 09A55058
(2). 'database', 'map'.
(2). 'type', 'string'.
(2). 'name', 'name'.
(2). 'sheet', 'areas'.
(1). '_db_name', 'map'.

Values for table: fetched:
(1). '1', table: 09A55030
(2). '_row_id', '1'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '2', table: 09A550A8
(2). '_row_id', '2'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '3', table: 09A550F8
(2). '_row_id', '3'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '4', table: 09A550D0
(2). '_row_id', '4'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '5', table: 09A55120
(2). '_row_id', '5'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '6', table: 09A55148
(2). '_row_id', '6'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '7', table: 09A55170
(2). '_row_id', '7'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '8', table: 09A55198
(2). '_row_id', '8'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '9', table: 09A551C0
(2). '_row_id', '9'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '10', table: 09A551E8
(2). '_row_id', '10'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '11', table: 09A55210
(2). '_row_id', '11'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '12', table: 09A55238
(2). '_row_id', '12'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '13', table: 09A55260
(2). '_row_id', '13'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '14', table: 09A55288
(2). '_row_id', '14'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '15', table: 09A552B0
(2). '_row_id', '15'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '16', table: 09A552D8
(2). '_row_id', '16'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '17', table: 09A55300
(2). '_row_id', '17'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '18', table: 09A55328
(2). '_row_id', '18'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '19', table: 09A55350
(2). '_row_id', '19'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '20', table: 09A55378
(2). '_row_id', '20'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '21', table: 09A553A0
(2). '_row_id', '21'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '22', table: 09A553C8
(2). '_row_id', '22'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '23', table: 09A553F0
(2). '_row_id', '23'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '24', table: 09A55418
(2). '_row_id', '24'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '25', table: 09A55440
(2). '_row_id', '25'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '26', table: 09A55468
(2). '_row_id', '26'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '27', table: 09A55490
(2). '_row_id', '27'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.
(1). '28', table: 09A554B8
(2). '_row_id', '28'.
(2). 'name', 'Room#1'.
(2). 'id', '1'.

IE, each time the script is run, it adds another identical record to the sheet, where I think it should be replacing the record instead. I've tried closing mudlet and deleting the database_map.db file.

Any ideas?

Yetzederixx
Posts: 186
Joined: Sun Nov 14, 2010 5:57 am

Re: database interface _unique not working

Post by Yetzederixx »

here's my code that works fine, perhaps it's with the REPLACE option?
Code: [show] | [select all] lua
function createDB()
	local created = db:create("wheredb", 
						{clanners = {name="", 
						 clan="", 
						 _unique = { "name" },
						 _violations = "IGNORE"
						}})

	if (not created) then echo("DB CREATION FAILED \n") end
end

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: database interface _unique not working

Post by Vadi »

I don't think so, because I use unique+replace in my dbs. I'll pull up my code and see the differences later in the day.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: database interface _unique not working

Post by Vadi »

Try using http://mudlet.org/asciidoc/manual.html#db_merge_unique over db:add. That's what I use all over the place.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: database interface _unique not working

Post by Vadi »

Looking at db and this example again, it seems while db:add was using INSERT OR REPLACE properly as it should have, db:create never actually created the id field with the UNIQUE property.

Next Mudlet update will have this fixed.

Post Reply