Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Primary key cannot be changed issue
  • I am new to Fuel, and I am working through the tutorials and documentation, however I'm stuck on the scaffolding part; everything seems to work until I try to do a save for an edit action. The table I have is quite simple, <code>
    CREATE TABLE IF NOT EXISTS `genres` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(50) COLLATE utf8_bin NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=22 ;
    </code> When I run <code>php oil g scaffold genres id:int name:string --no-timestamp</code> It creates the basic scaffold, cool; I can view, delete and add; but not edit. When I try to edit, I get: <code>Fuel\Core\FuelException [ Error ]: Primary key cannot be changed.
    throw new \FuelException('Primary key cannot be changed.');</code> But the genre table does not have name as a primary key. The code for the model file is, <code>use Orm\Model; class Model_Genre extends Model
    {
    protected static $_properties = array(
    'id',
    'name',
    ); protected static $_primary_key = array('id');
    public static function validate($factory)
    {
    $val = Validation::forge($factory);
    $val->add_field('id', 'Id', 'required|valid_string[numeric]');
    $val->add_field('name', 'Name', 'required|max_length[255]'); return $val;
    } }
    </code> I don't see any references there to the name field being a primary key; so what is causing this and how do I resolve it? Thanks.
  • It does have a primary key, which is your 'id' column. It is even defined as such in your model. I don't see any reference to the 'name' field in the error message, so I don't know why you think that is causing it. In ORM every table has a primary key and once assigned you can no longer change it.
  • Hi there, does this also mean if you try to delete files and then re-use oil to make the ORM controllers, models, etc? Maybe I did it wrong and will try it again from scratch ... Edit. I deleted all the genre models, etc and tried again, I made double sure that they did not exists and tried again, `php oil g scaffold genres name:string --no-timestamp -f` This worked! For some reason I can now edit & save, add, view and delete! Thanks

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion