John Smith
John Smith

Reputation: 6197

Symfony 1.4 Propel: I defined, but foreign keys isn't defined in tables

Lets see this schema:

  orders:
    _attributes:    { phpName: Orders, default_table_charset: utf8, default_table_collate: utf8_general_ci }
    id:             ~
    category_id:    { type: integer, size: '10', required: true, defaultvalue: '0', foreigntable: categories, foreignreference: id }
    whatisthis:     { type: longvarchar, required: false }

  categories:
    _attributes: { phpName: Categories }
    id:          ~
    name:        { type: varchar, size: '255', required: true, defaultvalue: '' }
    _uniques:    { index_name: [name] }

I figured out that the foreign keys are not defined in my tables. Im using innoDB so PKs are possible. Or Propel isnt supposed to set foreign keys?

Upvotes: 0

Views: 503

Answers (2)

Gianluca78
Gianluca78

Reputation: 784

Which symfony and propel version are you using?

Supposing that are using Propel 1.x, here you are a one-many relationship example:


  musician:
    _attributes: { phpName: Musician }
    id: { phpName: Id, type: INTEGER, size: '9', primaryKey: true, autoIncrement: true, required: true }
    #other fields
  musician_album:
    _attributes: { phpName: MusicianAlbum }
    id: { phpName: Id, type: INTEGER, size: '9', primaryKey: true, autoIncrement: true, required: true }
    musician_id: { phpName: MusicianId, type: INTEGER, size: '9', required: true, foreignTable: musician, foreignReference: id, onDelete: CASCADE, onUpdate: CASCADE }
    #other fields
    _indexes: { musician_id: [musician_id] }

To be honest, I prefer to manually create the database tables (setting, indexes, foreign keys and so on) and then I use the symfony (assuming that you are using Symfony 1.x) task to create the schema:

symfony propel:build-schema

Upvotes: 1

user1150508
user1150508

Reputation: 216

The "foreingtable" and "foreignreference" should be camel cased. And the category_id' size should be equal with the categories's id's size.

  orders:
    _attributes:    { phpName: Orders, default_table_charset: utf8, default_table_collate: utf8_general_ci }
    id:             ~
    category_id:    { type: integer, size: '10', required: true, defaultvalue: '0', foreignTable: categories, foreignreRerence: id }
    whatisthis:     { type: longvarchar, required: false }

  categories:
    _attributes: { phpName: Categories }
    id:          ~
    name:        { type: varchar, size: '255', required: true, defaultvalue: '' }
    _uniques:    { index_name: [name] }

Upvotes: 3

Related Questions