Welcome to the CodeSmith Community!

Re: No associations generated

Template Frameworks

A description has not yet been added to this group.

No associations generated

Answered (Verified) This question is answered

Hi,

I'm new to Codesmith/Nhibernate although I've been debugging through the NhibernateHelper source and templates to get some idea of what's going on. I'm hoping someone will point me in the right direction now.

I've generated C# code (entities, hbm files etc.) against my (Postgres 9) DB and the unit tests run OK against the simplest tables which don't have FKs to other entities. However, I notice that for entities which have FK columns, I get no associations generated in the hbm.xml files nor in the C# business objects. 

My schema is quite large but as a simple example, the following tables exist, a Model, which can have many ModelCells, which in turn can own several CellVertex records, hence ModelCell carries a "ModelID" as FK to Model.ID and CellVertex carries a "ModelCellID" as the FK to ModelCell.ID :

CREATE TABLE "Model"
(
  "ID" smallint NOT NULL DEFAULT nextval('seq_model'::regclass), -- The unique ID of the model.
  "DomainID" smallint NOT NULL DEFAULT nextval('seq_domain'::regclass), 
  "Name" character varying(64) NOT NULL, 
  "ModelVersion" smallint NOT NULL,    .... (extra columns removed here)
  CONSTRAINT "pkModel" PRIMARY KEY ("ID"),
  CONSTRAINT "ModelIsOfADomain" FOREIGN KEY ("DomainID")
      REFERENCES "Domain" ("ID") MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);

CREATE TABLE "ModelCell"
(
  "ID" bigint NOT NULL DEFAULT nextval('seq_model_cell'::regclass), -- The unique ID for the cell.
  "ModelID" smallint NOT NULL DEFAULT nextval('seq_model'::regclass), -- The ID of the model to which the cell belongs.
  "CellID" character varying(32) NOT NULL, .
  CONSTRAINT "pkCell" PRIMARY KEY ("ID"),
  CONSTRAINT "ModelHasModelCells" FOREIGN KEY ("ModelID")
      REFERENCES "Model" ("ID") MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);

CREATE TABLE "CellVertex"
(
  "ID" bigint NOT NULL DEFAULT nextval('seq_cell_vertex'::regclass), -- The unique ID of the cell vertex.
  "ModelCellID" bigint NOT NULL DEFAULT nextval('seq_model_cell'::regclass), -- The cell to which the vertex belongs.
  "PolygonID" integer NOT NULL, 
  "PointID" integer NOT NULL, 
  CONSTRAINT "pkCellVertex" PRIMARY KEY ("ID"),
  CONSTRAINT "ModelCellHasModelCellVertices" FOREIGN KEY ("ModelCellID")
      REFERENCES "ModelCell" ("ID") MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);

If I debug, when I step into EntityManager.cs GetManyToOne() I can see that

  • when _sourceTable = "ModelCell", _sourceTable.ForeignKeys contains 1 item, where (ForeignKeyMemberColumns = "ModelCell.ID", ForeignKeyTable = "ModelCell", PrimaryKey = "Model.ID", PrimaryKeyTable = "Model")
  • when _sourceTable = "CellVertex", _sourceTable.ForeignKeys contains 1 item, where (ForeignKeyMemberColumns = "CellVertex.ID", ForeignKeyTable = "CellVertex", PrimaryKey relates to "ModelCell.ID", PrimaryKeyTable = "ModelCell")

This seems to mean that at neither end of the relationship does the code get inside the following if statement in that method (because IsPrimaryKeyMember always seems to be true):

if

 

(!column.IsPrimaryKeyMember && !_associationMap.ContainsKey(column) && !NHibernateHelper.IsExcludedColumn(column.Name))

...and so I don't get associations created, as far as I can see.

Can anyone please tell me what I'm doing wrong? This is Codesmith 5.3.1 and using NHibernate 2.1.0.  Many thanks for reading.

  • Post Points: 5
Verified Answer
  • I've determined the cause of my issue - PK columns with the same name in 2 tables with a relationship between them. Something (SchemaExplorer?) doesn't like this and the associations are not created by the Codesmith NHibernateHelper code.

    I created a simple test schema, comprising 4 tables, model, model_cell, model2 and model_cell2:

    - model_cell has PK column "cell_id" and has an FK column "model_id" referencing model's PK column "model_id".

    - model_cell2 has PK column "id" and has an FK column "model_id" referencing model's PK column "id" (i.e. both model2 and model_cell2 have PK columns with the same name).

    When I run Codesmith, I get associations generated for the model  to model_cell relationships, but not for model2 to model_cell2 relationships.

    I'm changing my main schema to ensure all PK columns have unique names. I guess this may be good DB design anyway but wasn't aware it was a specific requirement for Codesmith to work.

    For info, SQL to generate my test schema is as follows:

    -- Create tables section -------------------------------------------------

    -- Table model

    CREATE TABLE "model"(

    "model_id" Serial,

    "model_name" Character(20)

    )

    WITH (OIDS=FALSE)

    ;

    -- Add keys for table model

    ALTER TABLE "model" ADD CONSTRAINT "Key1" PRIMARY KEY ("model_id")

    ;

    -- Table model_cell

    CREATE TABLE "model_cell"(

    "cell_id" Serial NOT NULL,

    "cell_name" Character(20),

    "model_id" Integer

    )

    WITH (OIDS=FALSE)

    ;

    -- Add keys for table model_cell

    ALTER TABLE "model_cell" ADD CONSTRAINT "Key2" PRIMARY KEY ("cell_id")

    ;

    -- Table model2

    CREATE TABLE "model2"(

    "id" Serial NOT NULL,

    "name" Character(20)

    )

    WITH (OIDS=FALSE)

    ;

    -- Add keys for table model2

    ALTER TABLE "model2" ADD CONSTRAINT "Key3" PRIMARY KEY ("id")

    ;

    -- Table model_cell2

    CREATE TABLE "model_cell2"(

    "id" Serial NOT NULL,

    "name" Character(20),

    "model_id" Integer

    )

    WITH (OIDS=FALSE)

    ;

    -- Add keys for table model_cell2

    ALTER TABLE "model_cell2" ADD CONSTRAINT "Key4" PRIMARY KEY ("id")

    ;

    -- Create relationships section -------------------------------------------------

    ALTER TABLE "model_cell" ADD CONSTRAINT "model_has_cells" FOREIGN KEY ("model_id") REFERENCES "model" ("model_id") ON DELETE NO ACTION ON UPDATE NO ACTION

    ;

    ALTER TABLE "model_cell2" ADD CONSTRAINT "model2_has_cells2" FOREIGN KEY ("model_id") REFERENCES "model2" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION

    ;

  • Hello,

    I debugged this down to a bug in the PostgreSchemaProvider. Please contact CodeSmith Support (www.codesmithtools.com/contactus) for a nightly build of CodeSmith Generator which resolves this issue.

    Thanks

    -Blake Niemyjski

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

All Replies
  • I looked at the schema explorer via Codesmith Studio on a colleague's machine (unfortunately I only have the standard license for the moment). In Schema Explorer, the FK column, e.g. ModelCellID on the CellVertex table, does not show with a key icon against it. The PK "ID" column is displayed with a gold key symbol, but according to the video I watched on using SchemaExplorer, there should be a silver key symbol against the FK column. I think this is another symptom of my problem.

    Please note, there is a glaring error in the CREATE TABLE SQL generated by TOAD which I pasted in my original post - the FK columns shouldn't default to use the sequence I specified for each PK. But my removing those defaults on FK columns doesn't seem to make a difference to the Codesmith behaviour I'm encountering here.

    Any suggestions?

  • I've determined the cause of my issue - PK columns with the same name in 2 tables with a relationship between them. Something (SchemaExplorer?) doesn't like this and the associations are not created by the Codesmith NHibernateHelper code.

    I created a simple test schema, comprising 4 tables, model, model_cell, model2 and model_cell2:

    - model_cell has PK column "cell_id" and has an FK column "model_id" referencing model's PK column "model_id".

    - model_cell2 has PK column "id" and has an FK column "model_id" referencing model's PK column "id" (i.e. both model2 and model_cell2 have PK columns with the same name).

    When I run Codesmith, I get associations generated for the model  to model_cell relationships, but not for model2 to model_cell2 relationships.

    I'm changing my main schema to ensure all PK columns have unique names. I guess this may be good DB design anyway but wasn't aware it was a specific requirement for Codesmith to work.

    For info, SQL to generate my test schema is as follows:

    -- Create tables section -------------------------------------------------

    -- Table model

    CREATE TABLE "model"(

    "model_id" Serial,

    "model_name" Character(20)

    )

    WITH (OIDS=FALSE)

    ;

    -- Add keys for table model

    ALTER TABLE "model" ADD CONSTRAINT "Key1" PRIMARY KEY ("model_id")

    ;

    -- Table model_cell

    CREATE TABLE "model_cell"(

    "cell_id" Serial NOT NULL,

    "cell_name" Character(20),

    "model_id" Integer

    )

    WITH (OIDS=FALSE)

    ;

    -- Add keys for table model_cell

    ALTER TABLE "model_cell" ADD CONSTRAINT "Key2" PRIMARY KEY ("cell_id")

    ;

    -- Table model2

    CREATE TABLE "model2"(

    "id" Serial NOT NULL,

    "name" Character(20)

    )

    WITH (OIDS=FALSE)

    ;

    -- Add keys for table model2

    ALTER TABLE "model2" ADD CONSTRAINT "Key3" PRIMARY KEY ("id")

    ;

    -- Table model_cell2

    CREATE TABLE "model_cell2"(

    "id" Serial NOT NULL,

    "name" Character(20),

    "model_id" Integer

    )

    WITH (OIDS=FALSE)

    ;

    -- Add keys for table model_cell2

    ALTER TABLE "model_cell2" ADD CONSTRAINT "Key4" PRIMARY KEY ("id")

    ;

    -- Create relationships section -------------------------------------------------

    ALTER TABLE "model_cell" ADD CONSTRAINT "model_has_cells" FOREIGN KEY ("model_id") REFERENCES "model" ("model_id") ON DELETE NO ACTION ON UPDATE NO ACTION

    ;

    ALTER TABLE "model_cell2" ADD CONSTRAINT "model2_has_cells2" FOREIGN KEY ("model_id") REFERENCES "model2" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION

    ;

  • Hello,

    Thanks for reporting this bug. Could you please also log it on our issue tracker and we will make sure we get this taken care of.

    Thanks

    -Blake Niemyjski

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

  • Thanks Blake - I've have just logged it.

    regards

    Stephen

  • Hello,

    I debugged this down to a bug in the PostgreSchemaProvider. Please contact CodeSmith Support (www.codesmithtools.com/contactus) for a nightly build of CodeSmith Generator which resolves this issue.

    Thanks

    -Blake Niemyjski

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

Page 1 of 1 (6 items)