status_id is set up as a foreign key in your database, which is why
you are getting the error... You are telling the form that it's not
mandatory, but you get the DB exception still because ultimately this
is where the constraint is.

So thats the why... How to deal with it maybe someone else can answer
better, but off the top of my head the first thing that comes to mind
is do you actually need it at all? If not, remove it from your
schema... But this begs the question why it is there in the first
place? If (as I assume) you do need it in some instances, but it's not
mandatory, perhaps you should just create a row in source_status with
an id of 0 (zero) and make the status_id field in your form hidden
with a default value of 0...

On May 11, 4:22 pm, nick <[email protected]> wrote:
> Suppose I have a table like this:
>
> | Field             | Type         | Null | Key | Default |
> +-------------------+--------------+------+-----+---------+
> | id                | int(11)      | NO   | PRI | NULL    |
> | subject           | varchar(255) | NO   |     | NULL    |
> | status_id         | int(11)      | NO   | MUL | NULL    |
> | reference_id      | int(11)      | NO   | MUL | NULL    |
>
> In the schema.yml, these fields were defined like this:
>
>   sources_updates:
>     id:                 ~
>     subject:            { type: varchar(255), required: true }
>     status_id :        { type: integer, foreignTable: sources_status,
> foreignReference: id, required: true }
>     reference_id:      { type: integer, foreignTable:
> sources_reference, foreignReference: id, required: true }
>     created_at:         ~
>     updated_at:         ~
>
> For a particular form, I don't need status_id or reference_id, so I
> unset() them in the form's configure() method.
>
> Since the schema says they are required, and the form base class also
> says they are required, I now need to ensure that they won't be
> required, so I do this in configure():
>
> $this->validatorSchema['reference_id'] = new sfValidatorPropelChoice
> (array('model' => 'SourcesUpdates', 'column' => 'id', 'required' =>
> false));
> $this->validatorSchema['status_id'] = new sfValidatorPropelChoice(array
> ('model' => 'SourcesUpdates', 'column' => 'id', 'required' =>
> false));
>
> Now, when creating a new record through the form, I get this error
> (which I think involves one of the indexes):
>
> Unable to execute INSERT statement. [wrapped: SQLSTATE[23000]:
> Integrity constraint violation: 1452 Cannot add or update a child row:
> a foreign key constraint fails (`hotsources/news_reports`, CONSTRAINT
> `news_reports_FK_7` FOREIGN KEY (`status_id`) REFERENCES
> `sources_status` (`id`))]
>
> How do I get around this error?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to