robertohuertasm / SQLite4Unity3d

SQLite made easy for Unity3d
MIT License
1.28k stars 265 forks source link

How to Specify a foreign key constraint? #62

Open sonaliee opened 7 years ago

sonaliee commented 7 years ago

I need to create a column with foreign key constraint,i have tried to make a table like #this

public class Student { [ForeignKey] public int Id{ get; set; } public string Name { get; set; } } This does not work.

daybson commented 4 years ago

Did you succeed on this? I'm struggled on the same problem. The only way I could made works is adding a new column on table by ADD COLUMN and then creating the REFERECE to another table, but my Character:

using (var con = new SQLiteConnection(DbURL, ConnectionMode))
            {
                var cmd = con.CreateCommand($"ALTER TABLE Character ADD COLUMN SuperPowerID INTEGER REFERENCES SuperPower(ID);");
                cmd.ExecuteNonQuery();
                con.Close();
            }

But, I can't put a SuperPowerID property on my c# Character class because will throw an error of "duplicate name SuperPowerID".

Also, there's no decorator flag to set to a property to generate and Foreing Key...

So, basically, I think there's no way to automatically generate FKs by decorating Properties...