Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » has_one relationship database error

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 448 Views
  • gh
    Avatar
    Community Member
    10 Posts

    has_one relationship database error Link to this post

    Hello

    Using SS 3.0.0-alpha-1 I am trying to link a page with its author:

    class AuthorPage extends Page {

       public static $has_one = array (
          'Author' => 'Member'
       );
       
       protected function onBeforeWrite () {
          $this->Author = Member::currentUser ();
          parent::onBeforeWrite ();
       }
    }

    When I add an AuthorPage in the CMS with the "add new" dialogue, I get the following error message:
    ---
    [User Error] Couldn't run query: INSERT INTO "AuthorPage" ("ID") VALUES (17) Cannot add or update a child row: a foreign key constraint fails ("silverstripe"."authorpage", CONSTRAINT "authorpage_ibfk_1" FOREIGN KEY ("AuthorID") REFERENCES "Member" ("ID"))
    POST /admin/pages/AddForm

    Line 562 in /Users/georg/silverstripe/sapphire/model/MySQLDatabase.php
    ---

    The error message is the same if the onBeforeWrite () function is missing in AuthorPage.

    What am I doing wrong here?
    Thanks for your hints.

    gh

  • gh
    Avatar
    Community Member
    10 Posts

    Re: has_one relationship database error Link to this post

    Does anyone know how this is supposed to work?
    Is there a bug in SS3.0.0-alpha1, or am I not doing it right?

    TIA,

    gh

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: has_one relationship database error Link to this post

    Should be...

    protected function onBeforeWrite () {
    $this->AuthorID = Member::currentUserID();
    parent::onBeforeWrite ();
    }

  • gh
    Avatar
    Community Member
    10 Posts

    Re: has_one relationship database error Link to this post

    Great, that works!
    Thank you very much!

    gh

    448 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.