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

  • SEARCH
  • Home
  • 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 8025529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:11:33+00:00 2026-06-04T23:11:33+00:00

In my database I have a table like this table foo int pk int

  • 0

In my database I have a table like this

table foo
int pk
int someFK NULL

with foreign key constraint on someFK and unique constraint on someFK.
This means in the MySQL database that I have that unless I specify NULL in someFK, there has to be a row in the corresponding table of course. However, I can also have several rows with NULL in someFK even if the unique constraint is on.

In my code, I use System.Data namespace and do like this:

DataTable table = new DataTable("Foo");

DataColumn col = null;

DataColumn[] primaryKey = new DataColumn[1];

col = table.Columns.Add(FooPropertyName, typeof(int));
col.Unique = true;
col.AutoIncrement = true;
primaryKey[0] = col;
table.PrimaryKey = primaryKey;

col = table.Columns.Add(SomeFkPropertyName, typeof(int));
col.Unique = true;
col.AutoIncrement = false;

However, if I add two DataRows to my DataTable, and those two have different primary keys but both have DBNull on the someFK column, I get an error message
Exception Type: System.Data.ConstraintException
Exception Message: Column ‘somefk’ is constrained to be unique. Value ” is already present.

This is not what I expect, so I was wondering if someone knows how to get around this (without removing the unique property)

  • 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-06-04T23:11:34+00:00Added an answer on June 4, 2026 at 11:11 pm

    You need to tell DataTable null values are accepted.

    col = table.Columns.Add(SomeFkPropertyName, typeof(int)); 
    col.Unique = true; 
    col.AutoIncrement = false; 
    col.AllowDBNull = true;
    

    More here AllowDBNull

    Edit 1

    You’re correct still broken,

            var table = new DataTable("Foo");
            table.Columns.AddRange(new []
            {
                new DataColumn("FooPropertyName", typeof(int))
                {
                    Unique = true,
                    AutoIncrement = true
                },
                new DataColumn("SomeFkPropertyName")
                {
                    Unique = true,
                    AllowDBNull = true
                },
            });
            table.PrimaryKey = new[] {table.Columns[0]};
    
            table.Rows.Add(0, 0);
            table.Rows.Add(1, 1);
            table.Rows.Add(2, DBNull.Value);
            table.Rows.Add(3, DBNull.Value); // Exception here
    

    Edit 2

    This didn’t work either :/

    private class MyDbNull
    {
        public static MyDbNull Value = new MyDbNull();
        public override bool Equals(object obj)
        {
            return false;
        }
    
        public override int GetHashCode()
        {
            return 0;
        }
    }
    
    table.Rows.Add(2, MyDbNull.Value);
    table.Rows.Add(3, MyDbNull.Value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in my postgresql 8.4 database like this: id(serial), event_type_id(id, foreign
i have database table like this +-------+--------------+----------+ | id | ip | date |
I have a table like this in my database (SQL Server 2008) ID Type
I have a database application in which a group is modeled like this: TABLE
I always forget how to do things like this. I have a database table
I have written a function to print database table to an array like this
I have a database table that has 4 fields. It is created like this:
Lets say I have table like this: String | Int1 | Int2 "foo" 5
I have an insert query for MySQL that looks like this: INSERT INTO table
I have a database table like this: id version_id field1 field2 1 1 texta

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.