lovasoa / SQLpage

Fast SQL-only data application builder. Automatically build a UI on top of SQL queries.
https://sql.datapage.app
MIT License
1.56k stars 89 forks source link

Issue on CRUD operations #294

Closed DavidLingier closed 5 months ago

DavidLingier commented 5 months ago

Introduction

I've tried to create a redirect on a "update" action, based on a working "delete" example. https://github.com/lovasoa/SQLpage/discussions/194 While testing this, the error message is returning instructions, but when applying these instructions the error remains the same.

To Reproduce

Starting from the "simple-website-example", I've changed the edit.sql file as follows:

select 'form' as component;
select 'text' as type, 'Username' as name, username as value
from users where id = $id;

update users set username = :Username
where id = $id and :Username is not null
RETURNING 'redirect' AS component, 'user.sql?id=' || $id as link;

Actual behavior

Error returned:

The redirect component cannot be used after data has already been sent to the client's browser. This component must be used before any other component. To fix this, either move the call to the 'redirect' component to the top of the SQL file, or create a new SQL file where 'redirect' is the first component.

Screenshots

update-redirect-issue-SQLPage

Expected behavior

Why does the redirect work on a "delete" and not on a "update"? How should the redirect be implemented, when placed on top of the .sql file?

Version information

lovasoa commented 5 months ago

Hello, welcome to SQLPage, and thank you for the clear report.

The redirect component must be the very first in a page. You are seeing an error because you have a form component before your redirect component. You have two solutions:

lovasoa commented 5 months ago

An example solution:

form.sql

select 
    'form' as component,
    'user.sql?id=' || $id as action;

select 'text' as type, 'Username' as name, username as value
from users where id = $id;

user.sql

update users set name=:Username where :Username is not null and and id=$id;

-- Your code that presents an user here 
DavidLingier commented 5 months ago

Thanks @lovasoa, with this input I was able to make it work.