# |
May 3rd 2019, 01:14 |
daniel.upshaw |
Why not do it this way though? ```CREATE TABLE `tests` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `parent_id` int(11) unsigned NOT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;``` |
# |
May 3rd 2019, 01:12 |
daniel.upshaw |
And also how you name the foreign keys.. but it looks like you've done that in your table |
# |
May 3rd 2019, 01:12 |
daniel.upshaw |
The trick may be in setting up your foreign key relationships |
# |
May 3rd 2019, 01:11 |
daniel.upshaw |
And you can still use your concatenated keys like normal, but they just aren't primary for that table.. they should still act the same way, but instead I think you would use both concatenated as a `UNIQUE` key |
# |
May 3rd 2019, 01:10 |
daniel.upshaw |
So the safest way I've found is to use an `id` field as your primary |
# |
May 3rd 2019, 01:10 |
daniel.upshaw |
And, it might not support it yet actually! Not sure how closely one has to follow a certain convention |
# |
May 3rd 2019, 01:09 |
daniel.upshaw |
That's right -- Ideally I would set it up that way... And it's probably possible to do this in a Cake way that supports it, but it's may take some digging to find out how |
# |
May 2nd 2019, 23:37 |
waspinator |
it's old, but should give you an idea of something to try |
# |
May 2nd 2019, 23:36 |
waspinator |
@noel https://stackoverflow.com/questions/8070914/how-to-implement-a-self-referencing-parent-id-model-in-cakephp |
# |
May 2nd 2019, 21:04 |
challgren |
@noel http://docs.phinx.org/en/latest/migrations.html |
# |
May 2nd 2019, 20:37 |
noel |
or for that matter how to write migrations / SQL CREATE statements for the things it does support? |
# |
May 2nd 2019, 20:36 |
noel |
@waspinator there isn’t much bake documentation… how would one tell if bake supports something? |
# |
May 2nd 2019, 20:30 |
waspinator |
if it bakes your models, make sure to remove any auto generated code which references a `parents` table |
# |
May 2nd 2019, 20:29 |
waspinator |
oh I see. I haven't had a chance to make one like that yet, so I'm not sure if it's supported by bake |
# |
May 2nd 2019, 20:28 |
noel |
Normal, non-self-referencing many to many associations work fine with bake. I’m not having trouble with those. It’s the self-referencing variety that I’m trying to conquer. |
# |
May 2nd 2019, 20:27 |
noel |
It’s supposed to be self-referencing. |
# |
May 2nd 2019, 20:26 |
noel |
But then it will look for the parents table. There is no parents table. |
# |
May 2nd 2019, 20:26 |
waspinator |
join tables should be named with their associated table names. so `tests_parents` instead of `tests_tests` |
# |
May 2nd 2019, 20:24 |
noel |
Same error message. |
# |
May 2nd 2019, 20:24 |
slackebot |
->create(); } public function down() { $this->table('tests')->drop()->save(); $this->table('tests_tests')->drop()->save(); } } ``` |
# |
May 2nd 2019, 20:24 |
slackebot |
=> false, ]) ->addPrimaryKey(['id']) ->addColumn('test_id', 'integer', [ 'default' => null, 'limit' => 11, 'null' => false, 'signed' => false, ]) ->addColumn('parent_id', 'integer', [ 'default' => null, 'limit' => 11, 'null' => false, 'signed' => false, ]) |
# |
May 2nd 2019, 20:24 |
slackebot |
->addPrimaryKey(['id']) ->addColumn('name', 'string', [ 'default' => null, 'limit' => 255, 'null' => true, ]) ->create(); $this->table('tests_tests') ->addColumn('id', 'integer', [ 'autoIncrement' => true, 'default' => null, 'limit' => 11, 'null' => false, 'signed' |
# |
May 2nd 2019, 20:24 |
noel |
Here’s the updated migration: ``` <?php use Migrations\AbstractMigration; class Initial extends AbstractMigration { public $autoId = false; public function up() { $this->table('tests') ->addColumn('id', 'integer', [ 'autoIncrement' => true, 'default' => null, 'limit' => 11, 'null' => false, 'signed' => false, ]) |
# |
May 2nd 2019, 20:24 |
noel |
@waspinator ok I can make it so.. but it still doesn’t fix the error :slightly_smiling_face: |
# |
May 2nd 2019, 20:23 |
waspinator |
there should only be a single primarykey, the `id` field. set use `$table->addIndex(['test_id', 'parent_id']);` instead |
# |
May 2nd 2019, 20:22 |
noel |
@waspinator tx for the feedback. it’s a link table for self-referenced many to many records from tests. You say it needs an id as well… why? It’s just a link table. Other many-to-many relationships work fine with Bake without their own id fields. Also adding the id field doesn’t fix the error. |
# |
May 2nd 2019, 20:16 |
waspinator |
your `tests_tests` table `belongsTo` the `tests` and `parent` tables, but it needs it's own `id` as well |
# |
May 2nd 2019, 20:15 |
waspinator |
normally every table should have an `id` column |
# |
May 2nd 2019, 20:14 |
waspinator |
what is `'tests_tests'` table supposed to be for? |
# |
May 2nd 2019, 20:11 |
slackebot |
->addColumn('parent_id', 'integer', [ 'default' => null, 'limit' => 11, 'null' => false, 'signed' => false, ]) ->addPrimaryKey(['test_id', 'parent_id']) ->create(); } public function down() { $this->table('tests')->drop()->save(); $this->table('tests_tests')->drop()->save(); } } ``` |
# |
May 2nd 2019, 20:11 |
slackebot |
->addPrimaryKey(['id']) ->addColumn('name', 'string', [ 'default' => null, 'limit' => 255, 'null' => true, ]) ->create(); $this->table('tests_tests') ->addColumn('test_id', 'integer', [ 'default' => null, 'limit' => 11, 'null' => false, 'signed' => false, ]) |
# |
May 2nd 2019, 20:11 |
noel |
@waspinator ``` <?php use Migrations\AbstractMigration; class Initial extends AbstractMigration { public $autoId = false; public function up() { $this->table('tests') ->addColumn('id', 'integer', [ 'autoIncrement' => true, 'default' => null, 'limit' => 11, 'null' => false, 'signed' => false, ]) |
# |
May 2nd 2019, 19:29 |
waspinator |
https://book.cakephp.org/3.0/en/migrations.html#generating-migrations-from-an-existing-database |
# |
May 2nd 2019, 19:28 |
waspinator |
@noel |
# |
May 2nd 2019, 19:28 |
waspinator |
user `bin/cake bake migration_snapshot Initial` to generate your table migrations from your database, and share them to help troubleshoot |
# |
May 2nd 2019, 16:42 |
noel |
I’ve tried removing the Primary keys – it doesn’t change the error.. so for some reason it’s still saying “some of the primary key values are missing” |
# |
May 2nd 2019, 16:40 |
noel |
@josbeir but I’ve selected tests from the select – which should define parent_id. Also having null values in a link table will cause any many to many relationship to break. |
# |
May 2nd 2019, 16:38 |
noel |
If not concatenated keys, then which should be primary? It’s a link table – my understanding is that for link tables both are primary. What do you suggest instead? |
# |
May 2nd 2019, 16:37 |
noel |
What do you mean by this? “That, and giving Cake the expected table names for the relationships” |
# |
May 2nd 2019, 16:36 |
noel |
@daniel.upshaw Yes I’m using the same table names as in the SO post. |
# |
May 2nd 2019, 16:24 |
daniel.upshaw |
And add `lft` and `rght` columns |