Users running a non-download version of Vanilla (pulled from github), on branch release/2019.016 or master from the last 2 weeks should upgrade to release/2019.017 or latest master for security reasons. Downloaded official open sources releases are not affected.

Foreign Key

How do I create a column with foreign key?


  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    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 Cheerleader & Troubleshooter Munich Moderator

Sign In or Register to comment.