Not signed in (Sign In)
Welcome Guest!
Want to take part in these discussions? If you have an account, sign in now.
If you don't have an account, apply for one now.
    • CommentAuthorsyllk
    • CommentTimeFeb 22nd 2008
     
    Hello,

    I've run into a problem where foreign key fields which can be nullable are being inserted with 0. Is there a way to make it insert with NULL instead? For instance,

    Model 1
    ================
    Machines
    id
    name

    Model 2
    ==============
    Tests
    id
    machine_id <--- this field is nullable

    The problem
    ==============
    When inserting a new test machine_id is always set to 0 when input is null.
    • CommentAuthorKaste
    • CommentTimeFeb 22nd 2008
     

    can't reproduce. unit-test? ticket?

    Are we talking about this? :

    $Test = $this->Test->create(array('machine_id'=>null));
    $Test>reload();
    
    $this->assertNull($Test->post_id);
    
    • CommentAuthorsyllk
    • CommentTimeFeb 23rd 2008
     

    What I meant was an empty foreign key is set always set to 0 even though it should be set to null. This is true for both inserts and update. For instance,

    function test_empty_foreign_key_set_to_null_on_create()  {
        $params = array('name'=>'Empty foreign key field should be set to NULL', 'machine_id'=>'');
        $Testrun =& new Testrun();
        $Testrun->setAttributes($params);
        $this->assertTrue($Testrun->save());
        $Testrun->reload();
        $this->assertNull($Testrun->get('machine_id'));
    }
    
    function test_empty_foreign_key_set_to_null_on_update() {
        $Testrun =& new Testrun();
        $Testrun->newRecord('id->','2','name->','All fields filled.','machine_id->','1');
        $this->assertTrue($Testrun->save());
    
        $Testrun->update(2, array('name'=>'machine_id should be NULL','machine_id'=>''));
        $tRecord = $Testrun->find(2);
        $this->assertNull($tRecord->machine_id);
    }
    

    Ticket # 129.

Add your comments
    Username Password
  • Format comments as