totersapp / asana-integration-test

0 stars 0 forks source link

relationships (between Tables) defined with eloquent are not all reflected in the database #6

Open abbood opened 5 years ago

abbood commented 5 years ago

The relationships (between Tables) defined with eloquent are not all reflected in the database Example: Store and OpCity Store.php

 public function opCity()
    {
        return $this->belongsTo(OpCity::class);
    }

OpCity.php

 public function stores()
    {
        return $this->hasMany(Store::class);
    }

If you check them in pgsql:

    sudo -u postgres psql postgres
    \connect toters-api
    \dt

SELECT
    tc.constraint_name, tc.table_name, kcu.column_name,
    ccu.table_name AS foreign_table_name,
    ccu.column_name AS foreign_column_name
FROM
    information_schema.table_constraints AS tc
    JOIN information_schema.key_column_usage AS kcu
      ON tc.constraint_name = kcu.constraint_name
    JOIN information_schema.constraint_column_usage AS ccu
      ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name='stores';

Why I am asking: I am on the seeding task, I generated schema visualization to get the relationships between the tables. So I can pick which tables to seed first.

abbood commented 5 years ago

➤ Mouafak Mkhallalati commented:

reason (why the relationship does not exist in our example) :

StoresForeignKey migration does not define a foreign key on op_city_id it define foreign key on city_id, currency_id but no on op_city_id migration file: 2015_09_30_150617_stores_foreign_key.php

abbood commented 5 years ago

➤ Jad Joubran commented:

Hi [~Mouafak] I originally worked on these migration files, here's a piece of information that may help: The reason why op_city_id doesn't have a Foreign Key at the database level, because it was recently added and I was in the process of migrating orders with null op_city_ids into op_city_id 1 which became Beirut