Creating 2 identical foreign key in a table

7 views
Skip to first unread message

C#_konqueror

unread,
Jan 16, 2012, 11:46:51 PM1/16/12
to Castle Project Users
Hi Guys,

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!



Mauricio Scheffer

unread,
Jan 16, 2012, 11:55:16 PM1/16/12
to castle-pro...@googlegroups.com
Could you post the full stack trace? Thanks.

--
Mauricio





--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.


C#_konqueror

unread,
Jan 17, 2012, 2:06:58 AM1/17/12
to Castle Project Users
Hi Mauricio

here is the inner exception
{"There is already an object named 'Country_ID' in the database. Could
not create constraint. See previous errors."}

Message
Could not create the schema

StackTrace
at Castle.ActiveRecord.ActiveRecordStarter.CreateSchema() in c:
\TeamCity\buildAgent\work\e41ee5ead2eba140\src\Castle.ActiveRecord
\Framework\ActiveRecordStarter.cs:line 290
at MyApp.Application_Start() in D:\TR Project\Global.asax.cs:line
53


On Jan 17, 12:55 pm, Mauricio Scheffer <mauricioschef...@gmail.com>
wrote:
> Could you post the full stack trace? Thanks.
>
> --
> Mauricio
>

Markus Zywitza

unread,
Jan 17, 2012, 5:17:02 AM1/17/12
to castle-pro...@googlegroups.com
Hi
 
that ForeignKey creates a constraint named Country_ID instead of selecting the PK of the referenced table. AFAIR it is used only to name the FK constraint other than FK__table__table__guid. But since these are two different constraints, each name must be named unique.
 
If you not intend to rename the constraint, simply remove the ForeignKey attribute.
 
-Markus

2012/1/17 C#_konqueror <jun2...@gmail.com>
Reply all
Reply to author
Forward
0 new messages