2

I'm trying to create some relational tables from 2 records but I keep getting the mysql error:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1005 Can't create table 'craft1.#sql-75c_603' (errno: 150)

The relationship is between 2 tables: quote and quotedItem. I based the code off the relationship seen here: http://www.yiiframework.com/doc/guide/1.1/en/database.arr between the Post and User tables.

The tables create fine without the relationship defined (I've also noticed they create with the relationship but I also get the above mysql error when installing the plugin so I presume it hasn't worked correctly).

I'm new to making plugins so there's probably something obvious I'm doing wrong but if someone could point me in the right direction that would be great.

Here's my code for quoted Item:

class ShoppingCart_QuotedItemRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'shoppingcart_quoteditems';
    }

    protected function defineAttributes()
    {
        return array(
            'quoteId' => AttributeType::String,
            'itemId' => AttributeType::String,
            'quantity' => AttributeType::String
        );
    }

    public function defineRelations()
    {
        return array(
            'quote' => array(static::BELONGS_TO, 'ShoppingCart_QuoteRecord', 'quoteId'),
        );
    }
}

And here's quote:

class ShoppingCart_QuoteRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'shoppingcart_quotes';
    }

    protected function defineAttributes()
    {
        return array(
            'title' => AttributeType::String,
            'country' => AttributeType::String,
            'name' => AttributeType::String,
            'email' => AttributeType::String,
            'company' => AttributeType::String,
            'phone' => AttributeType::String,
            'department' => AttributeType::String,
            'reference' => AttributeType::String,
            'questions' => AttributeType::String
        );
    }

    public function defineRelations()
    {
        return array(
            'quotedItems' => array(static::HAS_MANY, 'ShoppingCart_QuotedItemsRecord', 'quoteId'),
        );
    }
}
1

Ok, so I figured it out.

The reason it fails to create the tables is because the foreign key has to be of the same type as the primary key it's relating to.

So I just changed:

'quoteId' => AttributeType::String,

to:

'quoteId' => AttributeType::Number,

And it worked!

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.