Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 257075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:04:01+00:00 2026-05-11T22:04:01+00:00

I use migrations a lot (only MySQL) and since SubSonic Migrations only allows to

  • 0

I use migrations a lot (only MySQL) and since SubSonic Migrations only allows to define the parent and the master column in CreateForeignKey, I cannot define the update/delete actions in a FK Relation.

However there a quite a number of scenarios where I, for instance, need to define a FK relation where, if I delete the parent record, all records in the child table should be set to null (default would be to restrict the delete).

I use a little function that does this for me.
However since this is totally MySQL specific, and it breaks the idea that stands behand migrations (to be DB independent), I decided not to provide a patch for this. So I post the code snippet here.

But if someone needs this, too. Feel free to use it.
There is only one drawback: Since sonic.exe reads the code file and complies it on the fly you have to paste the code into every migration where you use it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:04:01+00:00Added an answer on May 11, 2026 at 10:04 pm

    Here’s a sample migration where I use this.
    In the down method you can just use the existing “DropForeignKey(…)” method, since the naming stays the same.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using SubSonic;
    
    namespace MyNameSpace.Migrations
    {
    
        public class Migration001 : Migration
        {
    
            public override void Up()
            {
                TableSchema.Table parent = GetTable("parent");
                TableSchema.Table child = GetTable("child");
    
                CreateForeignKeyMySQL(parent.GetColumn("id"), child.GetColumn("parent_id"),
                    CreateForeignKeyAction.SetNull, CreateForeignKeyAction.Restrict);
    
                base.Up();
            }
    
            public override void Down()
            {
                DropForeignKey(parent.GetColumn("id"), child.GetColumn("parent_id"));
    
                base.Down();
            }
    
            #region foreign key helper function
    
            public enum CreateForeignKeyAction
            {
                Cascade,
                Restrict,
                SetNull,
                NoAction
            }
    
            private String CreateForeignKeyActionValue(CreateForeignKeyAction action)
            {
                switch (action)
                {
                    case CreateForeignKeyAction.Cascade:
                        return "CASCADE";
                    case CreateForeignKeyAction.Restrict:
                        return "RESTRICT";
                    case CreateForeignKeyAction.SetNull:
                        return "SET NULL";
                    case CreateForeignKeyAction.NoAction:
                        return "NO ACTION";
                    default:
                        return "CASCADE";
                }
            }
    
            public void CreateForeignKeyMySQL(
                TableSchema.TableColumn oneTable, TableSchema.TableColumn manyTable,
                CreateForeignKeyAction onDelete, CreateForeignKeyAction onUpdate)
            {
    
                String sqlAppend = String.Format(" ON DELETE {0} ON UPDATE {1}",
                    CreateForeignKeyActionValue(onDelete), CreateForeignKeyActionValue(onUpdate));
    
                SubSonic.MySqlGenerator generator = new SubSonic.MySqlGenerator(null);
                String sqlCommand =
                    System.Text.RegularExpressions.Regex.Replace(
                        generator.BuildForeignKeyStatement(oneTable, manyTable), ";?$", sqlAppend
                    );
    
                Execute(sqlCommand);
            }
    
            #endregion
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to use EntityFramework 4.3 beta with migrations with MySql database?
situation We use migrations to update the database schema and configuration files for all
I have a non-rails application that I want to use rails active-record migrations with.
I have a java application, and I want to use rails migrations for this.
Having some trouble with the Migrations in Sequel and could use another set of
I use both ruby on rails and Java. I really enjoy using migrations when
I am trying to use mongoid_fulltext and can't seem to get the migrations to
I try to use first time MongoDB instead of MySQL in my Rails 3.1
I would like to use the new migration feature of EF, but on my
i have this crazy label for each time i create a migration that use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.