How to delete one record using frappe.db.delete syntax? - erpnext

I’m looking to use frappe.db.delete to remove the most recently modified record in a custom table, Warehouse Locations. I want to limit the delete to only one record that matches some filters.
The table is a child table, if that matters.
I am not clear on how to filter one record, based on the “modified” value. I tried:
frappe.db.delete(‘Warehouse Locations’,
{“warehouse”: warehouse,
“parent”: item_code,
“shelf”: shelf,
“modified”:("=",last_record_to_keep[0].modified)})
I am getting a syntax error when I run the above query.

First, filter out the record to be deleted using ORM by running
record = frappe.get_list('Warehouse Locations', order_by='-modified')[0]
Once you filtered it out, you can delete it using frappe.db.delete.
frappe.db.delete('Warehouse Locations', record)

I think the solution answered by #ChillarAnand is helpful.
Instead, I would like to give a different way to solve the problem you faced.
Per your question, the goal is to delete only one record from Warehouse Locations (Child Table).
# 1. Get the parent document for the warehouse location.
parent_doc = frappe.get_doc("Doctype", docname)
# 2. iterate through the child table rows to find the row meet your filter
# and assign to row_to_detele for late use or you can delete straight away
row_to_delete = ""
for row in parent_doc.warehouse_locations:
if row.modified == last_record_to_keep[0].modified:
row_to_delete = row.name
break
# 3. to remove the child table from the parent doc method
parent_doc.remove(row_to_delete)
For the document of parent_doc.remove(), you can find it through the below github path: https://github.com/frappe/frappe/blob/6b91ade73c07dc1c070ed137cf54a29a3e7b0993/frappe/model/base_document.py#L210 (7 Oct, 2021)

Related

Alternative way to list all part tables that have existing entries/tuples

I have one single master Subject table which have several part tables. One convenient thing about Table.delete() is that is displays a prompt with all existing entries that were created under that Subject. Aside from delete() is there an alternative way to print what part table entries were created under a single Subject entry?
Thank you
To get the entry count of all the part tables restricted by some restriction (e.g. subject_name), you can do something like this:
restriction = {'subject_name': 'my_star_subject'}
for part_table in Subject.parts(as_objects=True):
part_table_query = part_table & restriction
print(f'{part_table.table_name}: len(part_table_query)')
It's a slow process, but I think I would do the following to see where entries were:
(subject.Subject & 'subject="<NAME>"').descendants(as_objects=True)
Not sure if you'd be better off with children (1 level down) or descendants (all the way down). delete gives the full set of descendants, using table.delete_quick(get_count=True).
EDIT:
To just get the counts, you might want:
[print(i.table_name,len(i)) for i in (subject.Subject & 'subject="<NAME>"').descendants(as_objects=True)]

insert or update parent id to reference child records on same table

I have a PostgreSQL table in an application that holds both parent and child records. There is a column in the table to reference the the parent id where applicable for each child record. The problem is I am trying to import data from an external source where the child record is made up of a sub number of the parent. eg parent_reference_id = 123456000000 and a child_reference record for this could 123456000001, 123456000002 and so on. The application itself generates a unique id for each record when I import the data and so its possible to import the child and parent records simultaneously, however the difficulty I'm facing is linking the application generated id for the parent record to the parent_reference_id for the corresponding child records. The only hook I have is that the 1st six digits of the child_value_reference match the 1st six digits of the parent_value_reference and I've tried something like foo = bar(left(value,6)||'000000'; to create a match. However, I don't know how to use this to return the unique_id in a meaningful way and update the matching records. I've tried temporary tables and cte, however my knowledge of postgres is limited and I can't seem to find a solution that fits my problem. Another thing to mention is that these groups can change with updates within the external data so i'd also need a solution to make those updates too. Thanks in advance, Crispian

Edit an entry and then delete it

I try to edit a postgresql entry and immediately delete it. The goal is that the field to be updated is used as a trigger parameter that logs the operation.
If I use a stored function, everything is working correctly. But if I use CTE I can not delete the record.
with tmp as (
update users set by_user_id = '0004' where id = '0006' returning id
)
delete from users
using tmp
where users.id = tmp.id;
http://sqlfiddle.com/#!17/b677a/1
Per the documentation:
Trying to update the same row twice in a single statement is not supported. Only one of the modifications takes place, but it is not easy (and sometimes not possible) to reliably predict which one. This also applies to deleting a row that was already updated in the same statement: only the update is performed.

iSQLOutput - Update only Selected columns

My flow is simple and I am just reading a raw file into a SQL table.
At times the raw file contains data corresponding to existing records. I do not want to insert a new record in that case and would only want to update the existing record in the SQL table. The challenge is, there is a 'record creation date' column which I initialize at the time of record creation. The update operation overwrites that column too. I just want to avoid overwriting that column, while updating the other columns from the information coming from the raw file.
So far I am having no idea about how to do that. Could someone make a recommendation?
I defaulted the creation column to auto-populate in the SQL database itself. And I changed my flow to just update the remaining records. Talend job is now not touching that column. Problem solved.
Yet another reminder of 'Simplification is underrated'. :)

How to create table occurrences for filtered data..?

I have a table called transactions. Within that is a field called ipn_type. I would like to create separate table occurrences for the different ipn types I may have.
For example, one value for ipn_type is "dispute". In the past I would create a global field called "rel_dispute" and I would populate that with the value of "dispute". Then I could create a new table occurrence of the transactions table, and make a relationship based on transactions::ipn_type = transactions::rel_dispute. This way only the dispute records would show up in my new table occurrence.
Not long ago, somebody pointed out to me that this is no longer necessary, and there is a simpler way to setup such a relationship to create a new table occurrence. I can't for the life of me remember how that was done, though.
Any information on this would be greatly appreciated. Thanks!
To show a found set of only one type, you must either perform a find or use the Go to Related Record script step to show only related records. What you describe as your previous setup fits the latter.
The simpler way is to perform a find - either on demand, or by a script triggered OnLayoutEnter.
The new 'easy' way is probably:
using one base relationship only and
filtering only the displaying portal by type. This can be done with a global field, a global variable containing current display type. Multiple portals with different filter conditions are possible as well.
~jens