konqueror
konqueror

Reputation: 1

Creating 2 Identical Foreign key in Castle Active Record

I have here 2 entities

let say

Flight flight_id - PK origin - FK1 destination - FK2

Countries country_id - PK country code

sample code Class Flight { public int ID {get; set; }

  [BelongsTo(Column = "Origin", ForeignKey = "country_id")]
  public Countries Origin {get; set;}

  [BelongsTo(Column = "destination", ForeignKey = "country_id")]
  public Countries Destination {get; set; }

}

I'm getting an error when creating a schema on Activerecord. What will be an alternative to this? thanks!

Upvotes: 0

Views: 233

Answers (1)

EggyBach
EggyBach

Reputation: 4228

You're naming the ForeignKey parameter in the BelongsTo attribute the same for both keys. This parameter is not the name of the column you want to use, but actually the name of the constraint that ActiveRecord uses to name it when creating the schema.

I've made some assumptions and expanded your code sample with an example that works:

[ActiveRecord]
public class Flight
{
    [PrimaryKey]
    public int Id { get; set; }

    [BelongsTo(Column = "Origin", ForeignKey = "country_id_origin")]
    public Countries Origin { get; set; }

    [BelongsTo(Column = "Destination", ForeignKey = "country_id_destination")]
    public Countries Destination { get; set; }
}

[ActiveRecord]
public class Countries 
{
    [PrimaryKey]
    public int country_id { get; set; }

    [Property]
    public string CountryName { get; set; }
}

Upvotes: 2

Related Questions