HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Foreign Key

How do I create a column with foreign key?


  • R_JR_J Ex-Fanboy Munich Admin

    Vanilla doesn't use foreign keys, that might be the reason why foreign keys are not directly supported in the database classes. But you can always use raw SQL (if you know what you are doing 😀)

    $px = Gdn::structure()->databasePrefix();
    $queryString = "ALTER TABLE {$px}Comment ADD FOREIGN KEY (DiscussionID) REFERENCES {$px}Discussion(DiscussionID);";
  • nithishnithish New
    edited October 2019

    $Structure = GDN::Structure();


    ->column('DiscussionID', 'int', false)


    $px = Gdn::structure()->databasePrefix();

    $queryString = "ALTER TABLE {$px}FK_TEST ADD  FOREIGN KEY (DiscussionID) REFERENCES {$px}Discussion(DiscussionID);";


    @R_J I ran above queries but I keep getting => errno:150 "Foreign key constraint is incorrectly formatted"

  • R_JR_J Ex-Fanboy Munich Admin


Sign In or Register to comment.