Subclassmap adds an unwanted column in my table schema

Michael's Avatar

Michael

25 Oct, 2010 01:48 PM via web

Hello,

I have the following mappings:

public class RosterMap : ClassMap<Roster>
{
    public RosterMap()
    {
        Id(tr => tr.Id)
            .GeneratedBy.Increment();

        References(tr => tr.Team)
            .Not.Nullable();

        HasMany(r => r.Players)
            .Inverse()
            .Cascade.AllDeleteOrphan();
    }
}

public class TeamRosterMap : SubclassMap<TeamRoster>
{
    public TeamRosterMap()
    {
        Map(r => r.IsDefault)
            .Not.Nullable();

        Map(r => r.Name)
            .Not.Nullable();
    }
}

public class MatchRosterMap : SubclassMap<MatchRoster>
{
    public MatchRosterMap()
    {
        References(rm => rm.Match)
            .Not.Nullable();
    }
}

which generated the following SQL schema:

create table "Roster" (
    Id INTEGER not null,
    Team_id INTEGER not null,
    primary key (Id)
)

create table "MatchRoster" (
    Roster_id INTEGER not null,
    Match_id INTEGER not null,
    primary key (Roster_id)
)

create table "TeamRoster" (
    Roster_id INTEGER not null,
    IsDefault INTEGER not null,
    Name TEXT not null,
    Team_id INTEGER not null,
    primary key (Roster_id)
)

Why does TeamRoster has the additional field Team_id, whereas MatchRoster doesn't?

This is a problem, because I get an exception when I want to persist an entity in the database:

System.Data.SQLite.SQLiteException: Abort due to constraint violation
TeamRoster.Team_id may not be NULL

Thanks for your help!

  1. Support Staff 2 Posted by James Gregory on 25 Oct, 2010 01:54 PM

    James Gregory's Avatar

    Looks like a bug. What version are you running?

  2. 3 Posted by Michael on 25 Oct, 2010 02:12 PM

    Michael's Avatar

    This doesn't solve my problem, but this may help: if I map again the reference to Team in TeamRoster, things work well:

    public class TeamRosterMap : SubclassMap<TeamRoster>
    {
        public TeamRosterMap()
        {
            Map(r => r.IsDefault)
                .Not.Nullable();
    
            Map(r => r.Name)
                .Not.Nullable();
    
            References(r => r.Team)
                .Not.Nullable();
        }
    }

    Any ideas?

  3. 4 Posted by Michael on 26 Oct, 2010 08:00 AM

    Michael's Avatar

    Hello,

    I'm using version 1.1

  4. 5 Posted by Michael on 26 Oct, 2010 08:51 AM

    Michael's Avatar

    I don't know if this is related, but I have another problem which looks like the previous one.

    With the following mappings:

    public class MatchMap : ClassMap<Match>
    {
        public MatchMap()
        {
            Id(p => p.Id)
                .GeneratedBy.Increment();
    
            References(m => m.HomeTeam)
                .Cascade.SaveUpdate()
                .Not.Nullable();
    
            References(m => m.RoadTeam)
                .Cascade.SaveUpdate()
                .Not.Nullable();
    
            References(m => m.HomeRoster)
                .Cascade.SaveUpdate();
    
            References(m => m.RoadRoster)
                .Cascade.SaveUpdate();
        }
    }
    
    public class TeamMap : ClassMap<Team>
    {
        public TeamMap()
        {
            Id(t => t.Id)
                .GeneratedBy.Increment();
    
            HasMany(t => t.Rosters)
                .AsSet()
                .Inverse()
                .Cascade.AllDeleteOrphan();
    
            HasMany(t => t.MatchesAtHome)
                .Inverse();
    
            HasMany(t => t.MatchesOnRoad)
                .Inverse();
        }
    }
    
    public class PlayerInTeamMap : ClassMap<PlayerInTeam>
    {
        public PlayerInTeamMap()
        {
            Id(pit => pit.Id)
                .GeneratedBy.Increment();
    
            Map(ptn => ptn.PlayerNumber)
                .Not.Nullable();
    
            References(pit => pit.Player)
                .Not.Nullable();
    
            References(pit => pit.Position);
    
            References(pit => pit.Roster)
                .Not.Nullable();
        }
    }
    
    public class RosterMap : ClassMap<Roster>
    {
        public RosterMap()
        {
            Id(tr => tr.Id)
                .GeneratedBy.Increment();
    
            References(tr => tr.Team)
                .Not.Nullable();
    
            HasMany(r => r.Players)
                .Inverse()
                .Cascade.AllDeleteOrphan();
        }
    }

    I have this schema:

    create table "Match" (
    Id INTEGER not null,
    HomeTeam_id INTEGER not null,
    RoadTeam_id INTEGER not null,
    HomeRoster_id INTEGER,
    RoadRoster_id INTEGER,
    primary key (Id)
    )
    
    create table "PlayerInTeam" (
    Id INTEGER not null,
    PlayerNumber INTEGER not null,
    Player_id INTEGER not null,
    Position_id INTEGER,
    Roster_id INTEGER not null,
    HomeRoster_id INTEGER,
    primary key (Id)
    )
    
    create table "Roster" (
    Id INTEGER not null,
    Team_id INTEGER not null,
    primary key (Id)
    )
    
    create table "TeamRoster" (
    Roster_id INTEGER not null,
    IsDefault INTEGER not null,
    Name TEXT not null,
    Team_id INTEGER not null,
    primary key (Roster_id)
    )
    
    create table "Team" (
    Id INTEGER not null,
    primary key (Id)
    )

    What's wrong is the HomeRoster_id field of the PlayerInTeam table, which is not mapped, and shouldn't be there.

    I hope this help :)

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What number comes after 20?