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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

has_one relationship database error


Go to End


4 Posts   783 Views

Avatar
gh

Community Member, 10 Posts

17 January 2012 at 2:27pm

Edited: 17/01/2012 2:34pm

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

Community Member, 10 Posts

20 January 2012 at 7:35pm

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

Forum Moderator, 1899 Posts

20 January 2012 at 10:29pm

Should be...

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

Avatar
gh

Community Member, 10 Posts

21 January 2012 at 9:16pm

Great, that works!
Thank you very much!

gh