Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

has_one relationship database error


Reply

4 Posts   460 Views

Avatar
gh

17 January 2012 at 2:27pm (Last edited: 17 January 2012 2:34pm), Community Member, 10 Posts

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

Avatar
gh

20 January 2012 at 7:35pm Community Member, 10 Posts

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

Avatar
swaiba

20 January 2012 at 10:29pm Forum Moderator, 1796 Posts

Should be...

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

Avatar
gh

21 January 2012 at 9:16pm Community Member, 10 Posts

Great, that works!
Thank you very much!

gh