Declare a variable as a ROWTYPE - Trigger function












0















In this problem, I've created a table called groups(id, name, group_id INTEGER NULLABLE,class_id INTEGER NULLABLE), the logic is to classify the data between groups, classes and subclasses. A group has many classes, and a class has many subclasses. I thought to make a trigger function in plpgsql that guarantees some conditions before insert or update data.



The conditions to classify the data are below.




  • Groups:

    the user needs to insert only the name value.
    group_id and clazz_id must be NULL values.


  • Classes:

    the user needs to insert only the name and group_id values.
    group_id must be an INTEGER NOT NULL value and clazz_id is a NULL VALUE.
    group_id must attend the conditions to classificate as a Group.


  • Subclasses:

    The user needs to insert only the name, group_id and clazz_id values. Thus, you can set the group_id value equals to the group_id who belongs to the clazz_id for guarantee that the condition is true.
    group_id must be the same group_id as the class_id setted.
    clazz_id must attend to the conditions to classificate as a Class.



.



CREATE FUNCTION emp_subclasse() RETURNS trigger AS $subclasse_verification$
DECLARE
group groups%ROWTYPE;
classe groups%ROWTYPE;
class_group groups%ROWTYPE;
BEGIN
group =(SELECT * FROM groups WHERE id=NEW.group_id);
classe =(SELECT * FROM groups WHERE id=NEW.clazz_id);
class_group =(SELECT * FROM groups WHERE id=classe.group_id);
IF NEW.clazz_id IS NOT NULL THEN
IF (classe.group_id) IS NULL THEN
RAISE EXCEPTION 'A CLASS MUST POINT TO A GROUP';
ELSIF classe.group_id IS NOT NULL THEN
NEW.group_id = classe.group_id;
IF class_group.group_id IS NOT NULL OR class_group.clazz_id IS NOT NULL THEN
RAISE EXCEPTION 'A GROUP CANNOT HAVE A GROUP_ID OR CLASS_ID POINTED';
END IF;
END IF;
IF classe.clazz_id IS NOT NULL THEN
RAISE EXCEPTION 'CANNOT CLASSIFY A CLASS AS A SUBCLASS';
END IF;
ELSIF NEW.clazz_id IS NULL THEN
IF NEW.group_id IS NOT NULL THEN
IF group.group_id IS NOT NULL OR grupo.clazz_id IS NOT NULL THEN
RAISE EXCEPTION 'A GROUP CANNOT HAVE A GROUP_ID OR CLASS_ID POINTED';
END IF;
END IF;
END IF;
END;
$subclasse_verification$ LANGUAGE 'plpgsql';

CREATE TRIGGER subclasse_verification BEFORE INSERT OR UPDATE ON groups
FOR EACH ROW EXECUTE PROCEDURE emp_subclasse();


However, when I try to insert values, I get the error [2018-11-20 16:35:20] [42601] ERROR: subquery must return only one column
[2018-11-20 16:35:20] WHERE: PL/pgSQL function emp_subclasse() line 7 at assignment










share|improve this question





























    0















    In this problem, I've created a table called groups(id, name, group_id INTEGER NULLABLE,class_id INTEGER NULLABLE), the logic is to classify the data between groups, classes and subclasses. A group has many classes, and a class has many subclasses. I thought to make a trigger function in plpgsql that guarantees some conditions before insert or update data.



    The conditions to classify the data are below.




    • Groups:

      the user needs to insert only the name value.
      group_id and clazz_id must be NULL values.


    • Classes:

      the user needs to insert only the name and group_id values.
      group_id must be an INTEGER NOT NULL value and clazz_id is a NULL VALUE.
      group_id must attend the conditions to classificate as a Group.


    • Subclasses:

      The user needs to insert only the name, group_id and clazz_id values. Thus, you can set the group_id value equals to the group_id who belongs to the clazz_id for guarantee that the condition is true.
      group_id must be the same group_id as the class_id setted.
      clazz_id must attend to the conditions to classificate as a Class.



    .



    CREATE FUNCTION emp_subclasse() RETURNS trigger AS $subclasse_verification$
    DECLARE
    group groups%ROWTYPE;
    classe groups%ROWTYPE;
    class_group groups%ROWTYPE;
    BEGIN
    group =(SELECT * FROM groups WHERE id=NEW.group_id);
    classe =(SELECT * FROM groups WHERE id=NEW.clazz_id);
    class_group =(SELECT * FROM groups WHERE id=classe.group_id);
    IF NEW.clazz_id IS NOT NULL THEN
    IF (classe.group_id) IS NULL THEN
    RAISE EXCEPTION 'A CLASS MUST POINT TO A GROUP';
    ELSIF classe.group_id IS NOT NULL THEN
    NEW.group_id = classe.group_id;
    IF class_group.group_id IS NOT NULL OR class_group.clazz_id IS NOT NULL THEN
    RAISE EXCEPTION 'A GROUP CANNOT HAVE A GROUP_ID OR CLASS_ID POINTED';
    END IF;
    END IF;
    IF classe.clazz_id IS NOT NULL THEN
    RAISE EXCEPTION 'CANNOT CLASSIFY A CLASS AS A SUBCLASS';
    END IF;
    ELSIF NEW.clazz_id IS NULL THEN
    IF NEW.group_id IS NOT NULL THEN
    IF group.group_id IS NOT NULL OR grupo.clazz_id IS NOT NULL THEN
    RAISE EXCEPTION 'A GROUP CANNOT HAVE A GROUP_ID OR CLASS_ID POINTED';
    END IF;
    END IF;
    END IF;
    END;
    $subclasse_verification$ LANGUAGE 'plpgsql';

    CREATE TRIGGER subclasse_verification BEFORE INSERT OR UPDATE ON groups
    FOR EACH ROW EXECUTE PROCEDURE emp_subclasse();


    However, when I try to insert values, I get the error [2018-11-20 16:35:20] [42601] ERROR: subquery must return only one column
    [2018-11-20 16:35:20] WHERE: PL/pgSQL function emp_subclasse() line 7 at assignment










    share|improve this question



























      0












      0








      0








      In this problem, I've created a table called groups(id, name, group_id INTEGER NULLABLE,class_id INTEGER NULLABLE), the logic is to classify the data between groups, classes and subclasses. A group has many classes, and a class has many subclasses. I thought to make a trigger function in plpgsql that guarantees some conditions before insert or update data.



      The conditions to classify the data are below.




      • Groups:

        the user needs to insert only the name value.
        group_id and clazz_id must be NULL values.


      • Classes:

        the user needs to insert only the name and group_id values.
        group_id must be an INTEGER NOT NULL value and clazz_id is a NULL VALUE.
        group_id must attend the conditions to classificate as a Group.


      • Subclasses:

        The user needs to insert only the name, group_id and clazz_id values. Thus, you can set the group_id value equals to the group_id who belongs to the clazz_id for guarantee that the condition is true.
        group_id must be the same group_id as the class_id setted.
        clazz_id must attend to the conditions to classificate as a Class.



      .



      CREATE FUNCTION emp_subclasse() RETURNS trigger AS $subclasse_verification$
      DECLARE
      group groups%ROWTYPE;
      classe groups%ROWTYPE;
      class_group groups%ROWTYPE;
      BEGIN
      group =(SELECT * FROM groups WHERE id=NEW.group_id);
      classe =(SELECT * FROM groups WHERE id=NEW.clazz_id);
      class_group =(SELECT * FROM groups WHERE id=classe.group_id);
      IF NEW.clazz_id IS NOT NULL THEN
      IF (classe.group_id) IS NULL THEN
      RAISE EXCEPTION 'A CLASS MUST POINT TO A GROUP';
      ELSIF classe.group_id IS NOT NULL THEN
      NEW.group_id = classe.group_id;
      IF class_group.group_id IS NOT NULL OR class_group.clazz_id IS NOT NULL THEN
      RAISE EXCEPTION 'A GROUP CANNOT HAVE A GROUP_ID OR CLASS_ID POINTED';
      END IF;
      END IF;
      IF classe.clazz_id IS NOT NULL THEN
      RAISE EXCEPTION 'CANNOT CLASSIFY A CLASS AS A SUBCLASS';
      END IF;
      ELSIF NEW.clazz_id IS NULL THEN
      IF NEW.group_id IS NOT NULL THEN
      IF group.group_id IS NOT NULL OR grupo.clazz_id IS NOT NULL THEN
      RAISE EXCEPTION 'A GROUP CANNOT HAVE A GROUP_ID OR CLASS_ID POINTED';
      END IF;
      END IF;
      END IF;
      END;
      $subclasse_verification$ LANGUAGE 'plpgsql';

      CREATE TRIGGER subclasse_verification BEFORE INSERT OR UPDATE ON groups
      FOR EACH ROW EXECUTE PROCEDURE emp_subclasse();


      However, when I try to insert values, I get the error [2018-11-20 16:35:20] [42601] ERROR: subquery must return only one column
      [2018-11-20 16:35:20] WHERE: PL/pgSQL function emp_subclasse() line 7 at assignment










      share|improve this question
















      In this problem, I've created a table called groups(id, name, group_id INTEGER NULLABLE,class_id INTEGER NULLABLE), the logic is to classify the data between groups, classes and subclasses. A group has many classes, and a class has many subclasses. I thought to make a trigger function in plpgsql that guarantees some conditions before insert or update data.



      The conditions to classify the data are below.




      • Groups:

        the user needs to insert only the name value.
        group_id and clazz_id must be NULL values.


      • Classes:

        the user needs to insert only the name and group_id values.
        group_id must be an INTEGER NOT NULL value and clazz_id is a NULL VALUE.
        group_id must attend the conditions to classificate as a Group.


      • Subclasses:

        The user needs to insert only the name, group_id and clazz_id values. Thus, you can set the group_id value equals to the group_id who belongs to the clazz_id for guarantee that the condition is true.
        group_id must be the same group_id as the class_id setted.
        clazz_id must attend to the conditions to classificate as a Class.



      .



      CREATE FUNCTION emp_subclasse() RETURNS trigger AS $subclasse_verification$
      DECLARE
      group groups%ROWTYPE;
      classe groups%ROWTYPE;
      class_group groups%ROWTYPE;
      BEGIN
      group =(SELECT * FROM groups WHERE id=NEW.group_id);
      classe =(SELECT * FROM groups WHERE id=NEW.clazz_id);
      class_group =(SELECT * FROM groups WHERE id=classe.group_id);
      IF NEW.clazz_id IS NOT NULL THEN
      IF (classe.group_id) IS NULL THEN
      RAISE EXCEPTION 'A CLASS MUST POINT TO A GROUP';
      ELSIF classe.group_id IS NOT NULL THEN
      NEW.group_id = classe.group_id;
      IF class_group.group_id IS NOT NULL OR class_group.clazz_id IS NOT NULL THEN
      RAISE EXCEPTION 'A GROUP CANNOT HAVE A GROUP_ID OR CLASS_ID POINTED';
      END IF;
      END IF;
      IF classe.clazz_id IS NOT NULL THEN
      RAISE EXCEPTION 'CANNOT CLASSIFY A CLASS AS A SUBCLASS';
      END IF;
      ELSIF NEW.clazz_id IS NULL THEN
      IF NEW.group_id IS NOT NULL THEN
      IF group.group_id IS NOT NULL OR grupo.clazz_id IS NOT NULL THEN
      RAISE EXCEPTION 'A GROUP CANNOT HAVE A GROUP_ID OR CLASS_ID POINTED';
      END IF;
      END IF;
      END IF;
      END;
      $subclasse_verification$ LANGUAGE 'plpgsql';

      CREATE TRIGGER subclasse_verification BEFORE INSERT OR UPDATE ON groups
      FOR EACH ROW EXECUTE PROCEDURE emp_subclasse();


      However, when I try to insert values, I get the error [2018-11-20 16:35:20] [42601] ERROR: subquery must return only one column
      [2018-11-20 16:35:20] WHERE: PL/pgSQL function emp_subclasse() line 7 at assignment







      database postgresql plpgsql database-trigger sql-function






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 0:42









      404

      3,0851726




      3,0851726










      asked Nov 22 '18 at 23:39









      Manoel NetoManoel Neto

      33




      33
























          1 Answer
          1






          active

          oldest

          votes


















          0














            group =(SELECT * FROM groups WHERE id=NEW.group_id);
          classe =(SELECT * FROM groups WHERE id=NEW.clazz_id);
          class_group =(SELECT * FROM groups WHERE id=classe.group_id);


          These subqueries are returning all columns (SELECT *). To populate the variables correctly, either select the row without expanding: group := (SELECT groups FROM groups WHERE ...) or do a SELECT INTO: SELECT * INTO group FROM groups WHERE ...






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439121%2fdeclare-a-variable-as-a-rowtype-trigger-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














              group =(SELECT * FROM groups WHERE id=NEW.group_id);
            classe =(SELECT * FROM groups WHERE id=NEW.clazz_id);
            class_group =(SELECT * FROM groups WHERE id=classe.group_id);


            These subqueries are returning all columns (SELECT *). To populate the variables correctly, either select the row without expanding: group := (SELECT groups FROM groups WHERE ...) or do a SELECT INTO: SELECT * INTO group FROM groups WHERE ...






            share|improve this answer




























              0














                group =(SELECT * FROM groups WHERE id=NEW.group_id);
              classe =(SELECT * FROM groups WHERE id=NEW.clazz_id);
              class_group =(SELECT * FROM groups WHERE id=classe.group_id);


              These subqueries are returning all columns (SELECT *). To populate the variables correctly, either select the row without expanding: group := (SELECT groups FROM groups WHERE ...) or do a SELECT INTO: SELECT * INTO group FROM groups WHERE ...






              share|improve this answer


























                0












                0








                0







                  group =(SELECT * FROM groups WHERE id=NEW.group_id);
                classe =(SELECT * FROM groups WHERE id=NEW.clazz_id);
                class_group =(SELECT * FROM groups WHERE id=classe.group_id);


                These subqueries are returning all columns (SELECT *). To populate the variables correctly, either select the row without expanding: group := (SELECT groups FROM groups WHERE ...) or do a SELECT INTO: SELECT * INTO group FROM groups WHERE ...






                share|improve this answer













                  group =(SELECT * FROM groups WHERE id=NEW.group_id);
                classe =(SELECT * FROM groups WHERE id=NEW.clazz_id);
                class_group =(SELECT * FROM groups WHERE id=classe.group_id);


                These subqueries are returning all columns (SELECT *). To populate the variables correctly, either select the row without expanding: group := (SELECT groups FROM groups WHERE ...) or do a SELECT INTO: SELECT * INTO group FROM groups WHERE ...







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 0:46









                404404

                3,0851726




                3,0851726






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439121%2fdeclare-a-variable-as-a-rowtype-trigger-function%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Redirect URL with Chrome Remote Debugging Android Devices

                    Dieringhausen