The only way I can find of adding new data to a TinyDB table is with the table.insert()
method. However this appends the entry to the end of the table, but I would like to maintain the sequence of entries and sometimes I need to insert into an arbitrary index in the middle of the table. Is there no way to do this?
Asked
Active
Viewed 360 times
0

Spencer
- 1,931
- 1
- 21
- 44
1 Answers
0
There is no way to do what you are asking. Normally, the default index created tracks insertion order. When you add data, it will go at the end. If you need to maintain a certain order, you could create a new property the handle that case, and retrieve with a sort on that property.
If you truly want to insert in a specific id, you would need to add some logic to cascade the documents down. The logic would flow as:
- Insert a new record which is equal to the last record.
- Then, go backwards and cascade the records to the new open location
- Stop when you get to the location you need, and update the record with what you want to insert by using the ID.
The performance would drag since you are having to shift the records down. There are other ways to maintain the list - it would be similar to inserting a record in the middle of an array. Similar methods would ally here. Good Luck!

eDog
- 173
- 1
- 5
-
Dang! Well guess I need to think about this a bit more... I suppose I could always add a "row ID" attribute, but that adds certain complexities. – Spencer Dec 02 '18 at 02:51