Server Action to Copy A Table Row

My users are able to copy various items within their database and then edit the copy… for example an event definition.

It would be really useful if there was a server action “Database Insert Copy” which allowed me to specify the id of a database row and have Wappler go off and copy that row (to the same table) and return me the new id… without me needing to specify maybe 20 column names to copy and then maintain if I add a column to the table.

At the moment the only way to do this is with a stored procedure, which I am trying to move away from using! :slight_smile:

It can be done with a custom query, eg:

CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM myTable WHERE id = 100;
ALTER TABLE tmptable_1 MODIFY id int(12) null;
UPDATE tmptable_1 SET id = NULL;
INSERT INTO myTable SELECT * FROM tmptable_1;
DROP TEMPORARY TABLE IF EXISTS tmptable_1;

(found here)

I agree it’s a often a useful thing to do, although I would rarely want every field copied. From my point of view, a new feature to do exactly what you’re asking for wouldn’t be very useful, unless it had additional options. I think a custom query is the best solution.

Well on the assumption that Wappler is to be easy to use as a low/no code solution, I’m not convinced that the above really fits that requirement!

I see your point. However, if a new option is created for every possible piece of SQL code, Wappler will become rather unwieldy/unusable. If/when a snippets feature is implemented, you will (hopefully) be able to click a button to insert this piece of code - or the particular version of it which suits your purpose.

As I suggested earlier, I think duplicating records is often not a good idea. In your example, it may be that several of the 20 columns might be modifying in the duplicate; it may that several columns must be modified. For example, if you duplicate a product (very likely a useful thing to do), and you rely on users to update the price, keywords, options etc, you will inevitably end up with errors (IMO). Creating a near-duplicate with certain fields empty will be a better solution. Of course, I’m not suggesting this applies to your specific case, but in general (again, IMO).

Wappler would stop being Wappler if it became a ‘no code’ solution.

Finally, I don’t use stored procedures very much, but I think there are cases where they are useful. (Don’t let @JonL bully you :slightly_smiling_face:)

2 Likes

Thanks for your feedback @TomD. I sense you won’t be voting for this one then… but I look forward to feedback from others who may.

Don’t worry, that will NEVER happen! :rofl:

Probably not… (Though I do see your point).

Looking at the page I linked to, I think there’s a slightly simpler solution:

CREATE TEMPORARY TABLE tmptable_1 SELECT * FROM myTable WHERE id = 100;
UPDATE tmptable_1 SET id = 0;
INSERT INTO myTable SELECT * FROM tmptable_1;
DROP TEMPORARY TABLE IF EXISTS tmptable_1;

(which makes it slighly less deserving of having its own server action step)

1 Like

Now that’s a new way of calling “free unsolicited professional advice”.

3 Likes