3

I got an issue with Propel fixtures loading in Symfony2. I have the following schema:

<table name="application" phpName="Application">

   <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
   <column name="name" type="varchar" required="true" primaryString="true" />

   <behavior name="timestampable" />

</table>

<table name="application_iphone" phpName="IphoneApplication">

   <column name="store_id" type="varchar" required="true" />

   <behavior name="concrete_inheritance">
       <parameter name="extends" value="application" />
   </behavior>

</table>

<table name="application_identifier" phpName="IphoneApplicationIdentifier">

    <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
    <column name="application_id" required="true" type="integer" />
    <column name="identifier" type="varchar" required="true" primaryString="true" />

    <foreign-key foreignTable="application_iphone">
        <reference local="application_id" foreign="id" />                                 
    </foreign-key>     

</table>

The model builds correctly. The issue raises when I try to load the following fixtures:

Acme\MyBundle\Model\Application:
    first_app:
        name: "MyFirstApp"
        descendant_class: "IphoneApplication"

Acme\MyBundle\Model\IphoneApplication:
    first_app_iphone:
        id: first_app
        store_id: 2342

Acme\MyBundle\Model\IphoneApplicationIdentifier:     
    first_app_identifier:
        identifier: "com.acme.myfirstapp.appstore"
        application_id: first_app_iphone

The following error occurs:

[Propel] Exception
Cannot insert a value for auto-increment primary key (application.ID)

I added my "first_app" application twice (one in Application, another time in IphoneApplication) to prevent from another issue with my IphoneApplicationIdentifier fixture:

[Propel] Exception The object "first_app" from class "Acme\MyBundle\Model\Application" is not defined in your data file.

How can I insert fixtures for a concrete inheritant model?

Jonathan Petitcolas
  • 4,254
  • 4
  • 31
  • 42

3 Answers3

3

Propel is telling you that you are trying to set a value for an autoincrement column, this is due to the

    id: first_app

line.

If I am understanding you correctly, you just want to add an Iphoneapplication item, is that right? If that is the case you just need to do:

Acme\MyBundle\Model\IphoneApplication:
    first_app_iphone:
        name: "MyFirstApp"
        store_id: 2342
Carlos Granados
  • 11,273
  • 1
  • 38
  • 44
  • I am not sure if it is clear in the answer but you don't need to add anything for the application table in the fixtures file, this is filled automatically due to the table inheritance – Carlos Granados Aug 07 '12 at 15:53
  • I agree with you. If there was only these two classes, everything would be alright. However, I did not mention another model, linked with a foreign key to my child class (I edited my question). The issue is that _IphoneApplicationIdentifier_ seems to expect an _Application_ object, and not a _IphoneApplication_ one in this case. That's why I mentionned twice my "first_app" application. – Jonathan Petitcolas Aug 07 '12 at 15:58
  • Issue fixed! The problem was caused by another fixture file. Thanks to help me to point it out. :) – Jonathan Petitcolas Aug 07 '12 at 16:13
1

I had similar situation and ended up with having different schema files. For testing one - I removed auto-increment from schema and defined another place for storing models. It was long time ago, but general steps were as below:


  1. Dump fixtures for prod environment

    app/console propel:fixtures:dump
    
  2. Build everything you need for testing database

    app/console propel:database:create --env=test
    app/console propel:sql:build --env=test
    app/console propel:sql:insert --force --env=test
    app/console propel:model:build --env=test
    
  3. Imported fixtures into test database

    app/console propel:fixtures:load --env=test
    

Note, that commands may vary depending on version of Propel

Vitalii Zurian
  • 17,858
  • 4
  • 64
  • 81
0

I found the mistake by explaining my problem in details. The issue did not come from this fixtures file. The error I had seemed strange to me. Why the error mentioned *first_app* instead of *iphone_first_app*?

After digging into the other fixtures files, I found a forgotten reference to *first_app*. The solution is simply the following (as mentioned by Carlos Granados):

Acme\MyBundle\Model\IphoneApplication:
    first_app_iphone:
        name: "My first app"
        store_id: 2342

Acme\MyBundle\Model\IphoneApplicationIdentifier:     
    first_app_identifier:
        identifier: "com.acme.myfirstapp.appstore"
        application_id: first_app_iphone

There is no need to define base class. :)

Jonathan Petitcolas
  • 4,254
  • 4
  • 31
  • 42