yii2: Support for missing sqlite3 ALTER TABLE commands
It looks like there is no support for creating foreign keys on sqlite3.
What steps will reproduce the problem?
Configure your yii2 app to use a sqlite3 database and create this migration
public function safeUp()
{
$this->createTable('parents', [
'id' => $this->primaryKey(),
]);
$this->createTable('children', [
'id' => $this->primaryKey(),
'parents_id' => $this->integer()->notNull(),
]);
// creates index for column lemas_id
$this->createIndex(
'yii2idx-children-parents_id',
'children',
'parents_id'
);
$this->addForeignKey(
'yii2fk-children-parents_id',
'children',
'parents_id',
'parents',
'id'
);
What is the expected result?
The tables and foreign keys are created in the sqlite3 database
What do you get instead?
*** applying m171023_034457_capel_create_parents_table
> create table parents ... done (time: 0.001s)
*** applied m171023_034457_capel_create_parents_table (time: 0.193s)
*** applying m171023_034458_capel_create_children_table
> create table children ... done (time: 0.001s)
> create index yii2idx-children-parents_id on children (parents_id) ... done (time: 0.000s)
> add foreign key yii2fk-children-parents_id: children (parents_id) references parents (id) ...Exception: yii\db\sqlite\QueryBuilder::addForeignKey is not supported by SQLite. (/home/santilin/devel/yii2base/vendor/yiisoft/yii2/db/sqlite/QueryBuilder.php:238)
Additional info
Q | A |
---|---|
Yii version | 2.0.13-dev |
PHP version | 7.0 |
Operating system | Debian |
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 19 (19 by maintainers)
Sure!
I have tested it and it works correctly. The code to parse the foreign key could be improved with anothe SqlTokenizer, but I have not found a good documentation on using SqlTokenizer.
I’m planning to make all the other DDL operations, creating a protected function with all the common code, as all the operations work more or less the same way.
Sorry to say that, but considering the complexity of the method this does not look like something we could reliably maintain within Yii core. It simulates a feature of sqlite, which is not natively supported so it could quite well exist as an extension providing additional functionality in case someone needs this. Its not a common problem so imo it is too special to be in Yii core.