Joining same table multiple times in ServiceStack.OrmLite












2














When joining a table to itself, the sql statment generated does not reference the tables correctly.



It's working when the "main" table is different from the joining table https://github.com/ServiceStack/ServiceStack.OrmLite#join-aliases



Class



public class Page
{
public string ActivityId { get; set; }
public int DefinitionId { get; set; }
public int PageId { get; set; }
}


Code



using (var db = connection.Open())
{
var sql = db.From<Page>()
.Join<Page>((p1, p2) =>
p1.DefinitionId == 349 &&
p1.ActivityId == "a633326227969545457" &&
p1.PageId == p2.PageId &&
p2.DefinitionId == 340, db.JoinAlias("p2"))
.Select<Page>(p => new {
String = Sql.JoinAlias(p.ActivityId, "p2")
});

}


SQL statement



p1.DefinitionId == 349 and p1.ActivityId == "a633326227969545457", these should not refer to p2



SELECT p2."ActivityId" AS String 
FROM "Page" INNER JOIN "Page" p2 ON (
((("p2"."DefinitionId" = 349)
AND ("p2"."ActivityId" = 'a633326227969545457'))
AND ("p2"."PageId" = "p2"."PageId"))
AND ("p2"."DefinitionId" = 340))


Is it a bug or am I missing something here?










share|improve this question





























    2














    When joining a table to itself, the sql statment generated does not reference the tables correctly.



    It's working when the "main" table is different from the joining table https://github.com/ServiceStack/ServiceStack.OrmLite#join-aliases



    Class



    public class Page
    {
    public string ActivityId { get; set; }
    public int DefinitionId { get; set; }
    public int PageId { get; set; }
    }


    Code



    using (var db = connection.Open())
    {
    var sql = db.From<Page>()
    .Join<Page>((p1, p2) =>
    p1.DefinitionId == 349 &&
    p1.ActivityId == "a633326227969545457" &&
    p1.PageId == p2.PageId &&
    p2.DefinitionId == 340, db.JoinAlias("p2"))
    .Select<Page>(p => new {
    String = Sql.JoinAlias(p.ActivityId, "p2")
    });

    }


    SQL statement



    p1.DefinitionId == 349 and p1.ActivityId == "a633326227969545457", these should not refer to p2



    SELECT p2."ActivityId" AS String 
    FROM "Page" INNER JOIN "Page" p2 ON (
    ((("p2"."DefinitionId" = 349)
    AND ("p2"."ActivityId" = 'a633326227969545457'))
    AND ("p2"."PageId" = "p2"."PageId"))
    AND ("p2"."DefinitionId" = 340))


    Is it a bug or am I missing something here?










    share|improve this question



























      2












      2








      2







      When joining a table to itself, the sql statment generated does not reference the tables correctly.



      It's working when the "main" table is different from the joining table https://github.com/ServiceStack/ServiceStack.OrmLite#join-aliases



      Class



      public class Page
      {
      public string ActivityId { get; set; }
      public int DefinitionId { get; set; }
      public int PageId { get; set; }
      }


      Code



      using (var db = connection.Open())
      {
      var sql = db.From<Page>()
      .Join<Page>((p1, p2) =>
      p1.DefinitionId == 349 &&
      p1.ActivityId == "a633326227969545457" &&
      p1.PageId == p2.PageId &&
      p2.DefinitionId == 340, db.JoinAlias("p2"))
      .Select<Page>(p => new {
      String = Sql.JoinAlias(p.ActivityId, "p2")
      });

      }


      SQL statement



      p1.DefinitionId == 349 and p1.ActivityId == "a633326227969545457", these should not refer to p2



      SELECT p2."ActivityId" AS String 
      FROM "Page" INNER JOIN "Page" p2 ON (
      ((("p2"."DefinitionId" = 349)
      AND ("p2"."ActivityId" = 'a633326227969545457'))
      AND ("p2"."PageId" = "p2"."PageId"))
      AND ("p2"."DefinitionId" = 340))


      Is it a bug or am I missing something here?










      share|improve this question















      When joining a table to itself, the sql statment generated does not reference the tables correctly.



      It's working when the "main" table is different from the joining table https://github.com/ServiceStack/ServiceStack.OrmLite#join-aliases



      Class



      public class Page
      {
      public string ActivityId { get; set; }
      public int DefinitionId { get; set; }
      public int PageId { get; set; }
      }


      Code



      using (var db = connection.Open())
      {
      var sql = db.From<Page>()
      .Join<Page>((p1, p2) =>
      p1.DefinitionId == 349 &&
      p1.ActivityId == "a633326227969545457" &&
      p1.PageId == p2.PageId &&
      p2.DefinitionId == 340, db.JoinAlias("p2"))
      .Select<Page>(p => new {
      String = Sql.JoinAlias(p.ActivityId, "p2")
      });

      }


      SQL statement



      p1.DefinitionId == 349 and p1.ActivityId == "a633326227969545457", these should not refer to p2



      SELECT p2."ActivityId" AS String 
      FROM "Page" INNER JOIN "Page" p2 ON (
      ((("p2"."DefinitionId" = 349)
      AND ("p2"."ActivityId" = 'a633326227969545457'))
      AND ("p2"."PageId" = "p2"."PageId"))
      AND ("p2"."DefinitionId" = 340))


      Is it a bug or am I missing something here?







      ormlite-servicestack






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 22:54

























      asked Nov 22 at 18:23









      Kai-Rune

      333413




      333413
























          1 Answer
          1






          active

          oldest

          votes


















          1














          In order to be able to use an alias on the source table you would need to set a table alias which wasn't supported until now with this commit.



          You can now use the new db.TableAlias() API (which is now preferable over JoinAlias()) on both the source table as well as any join tables, e.g:



          var q = db.From<Page>(db.TableAlias("p1"))
          .Join<Page>((p1, p2) =>
          p1.DefinitionId == 349 &&
          p1.ActivityId == "a633326227969545457" &&
          p1.PageId == p2.PageId &&
          p2.DefinitionId == 340, db.TableAlias("p2"))
          .Select<Page>(p => new {
          String = Sql.TableAlias(p.ActivityId, "p2")
          });

          var rows = db.Select(q);


          This change is available from v5.4.1 that's now available on MyGet.






          share|improve this answer





















          • mythz I am using 5.4.1 and I get the error. 'TableOptions' does not contain a definition for 'Select' and the best extension method overload 'OrmLiteReadApi.Select<Company>(IDbConnection, string)' requires a receiver of type 'IDbConnection'
            – Steve Coleman
            Dec 7 at 23:04












          • @SteveColeman sounds like your code is wrong, db.TableAlias() is only an argument and .Select() should only be called on the SqlExpression not TableAlias(). You can check out the commit for more usage examples.
            – mythz
            Dec 8 at 2:44










          • :-( ... I was missing a ) It was a long day of coding. Thanks for the response.
            – Steve Coleman
            Dec 9 at 16:33











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436421%2fjoining-same-table-multiple-times-in-servicestack-ormlite%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          In order to be able to use an alias on the source table you would need to set a table alias which wasn't supported until now with this commit.



          You can now use the new db.TableAlias() API (which is now preferable over JoinAlias()) on both the source table as well as any join tables, e.g:



          var q = db.From<Page>(db.TableAlias("p1"))
          .Join<Page>((p1, p2) =>
          p1.DefinitionId == 349 &&
          p1.ActivityId == "a633326227969545457" &&
          p1.PageId == p2.PageId &&
          p2.DefinitionId == 340, db.TableAlias("p2"))
          .Select<Page>(p => new {
          String = Sql.TableAlias(p.ActivityId, "p2")
          });

          var rows = db.Select(q);


          This change is available from v5.4.1 that's now available on MyGet.






          share|improve this answer





















          • mythz I am using 5.4.1 and I get the error. 'TableOptions' does not contain a definition for 'Select' and the best extension method overload 'OrmLiteReadApi.Select<Company>(IDbConnection, string)' requires a receiver of type 'IDbConnection'
            – Steve Coleman
            Dec 7 at 23:04












          • @SteveColeman sounds like your code is wrong, db.TableAlias() is only an argument and .Select() should only be called on the SqlExpression not TableAlias(). You can check out the commit for more usage examples.
            – mythz
            Dec 8 at 2:44










          • :-( ... I was missing a ) It was a long day of coding. Thanks for the response.
            – Steve Coleman
            Dec 9 at 16:33
















          1














          In order to be able to use an alias on the source table you would need to set a table alias which wasn't supported until now with this commit.



          You can now use the new db.TableAlias() API (which is now preferable over JoinAlias()) on both the source table as well as any join tables, e.g:



          var q = db.From<Page>(db.TableAlias("p1"))
          .Join<Page>((p1, p2) =>
          p1.DefinitionId == 349 &&
          p1.ActivityId == "a633326227969545457" &&
          p1.PageId == p2.PageId &&
          p2.DefinitionId == 340, db.TableAlias("p2"))
          .Select<Page>(p => new {
          String = Sql.TableAlias(p.ActivityId, "p2")
          });

          var rows = db.Select(q);


          This change is available from v5.4.1 that's now available on MyGet.






          share|improve this answer





















          • mythz I am using 5.4.1 and I get the error. 'TableOptions' does not contain a definition for 'Select' and the best extension method overload 'OrmLiteReadApi.Select<Company>(IDbConnection, string)' requires a receiver of type 'IDbConnection'
            – Steve Coleman
            Dec 7 at 23:04












          • @SteveColeman sounds like your code is wrong, db.TableAlias() is only an argument and .Select() should only be called on the SqlExpression not TableAlias(). You can check out the commit for more usage examples.
            – mythz
            Dec 8 at 2:44










          • :-( ... I was missing a ) It was a long day of coding. Thanks for the response.
            – Steve Coleman
            Dec 9 at 16:33














          1












          1








          1






          In order to be able to use an alias on the source table you would need to set a table alias which wasn't supported until now with this commit.



          You can now use the new db.TableAlias() API (which is now preferable over JoinAlias()) on both the source table as well as any join tables, e.g:



          var q = db.From<Page>(db.TableAlias("p1"))
          .Join<Page>((p1, p2) =>
          p1.DefinitionId == 349 &&
          p1.ActivityId == "a633326227969545457" &&
          p1.PageId == p2.PageId &&
          p2.DefinitionId == 340, db.TableAlias("p2"))
          .Select<Page>(p => new {
          String = Sql.TableAlias(p.ActivityId, "p2")
          });

          var rows = db.Select(q);


          This change is available from v5.4.1 that's now available on MyGet.






          share|improve this answer












          In order to be able to use an alias on the source table you would need to set a table alias which wasn't supported until now with this commit.



          You can now use the new db.TableAlias() API (which is now preferable over JoinAlias()) on both the source table as well as any join tables, e.g:



          var q = db.From<Page>(db.TableAlias("p1"))
          .Join<Page>((p1, p2) =>
          p1.DefinitionId == 349 &&
          p1.ActivityId == "a633326227969545457" &&
          p1.PageId == p2.PageId &&
          p2.DefinitionId == 340, db.TableAlias("p2"))
          .Select<Page>(p => new {
          String = Sql.TableAlias(p.ActivityId, "p2")
          });

          var rows = db.Select(q);


          This change is available from v5.4.1 that's now available on MyGet.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 at 17:57









          mythz

          117k14193333




          117k14193333












          • mythz I am using 5.4.1 and I get the error. 'TableOptions' does not contain a definition for 'Select' and the best extension method overload 'OrmLiteReadApi.Select<Company>(IDbConnection, string)' requires a receiver of type 'IDbConnection'
            – Steve Coleman
            Dec 7 at 23:04












          • @SteveColeman sounds like your code is wrong, db.TableAlias() is only an argument and .Select() should only be called on the SqlExpression not TableAlias(). You can check out the commit for more usage examples.
            – mythz
            Dec 8 at 2:44










          • :-( ... I was missing a ) It was a long day of coding. Thanks for the response.
            – Steve Coleman
            Dec 9 at 16:33


















          • mythz I am using 5.4.1 and I get the error. 'TableOptions' does not contain a definition for 'Select' and the best extension method overload 'OrmLiteReadApi.Select<Company>(IDbConnection, string)' requires a receiver of type 'IDbConnection'
            – Steve Coleman
            Dec 7 at 23:04












          • @SteveColeman sounds like your code is wrong, db.TableAlias() is only an argument and .Select() should only be called on the SqlExpression not TableAlias(). You can check out the commit for more usage examples.
            – mythz
            Dec 8 at 2:44










          • :-( ... I was missing a ) It was a long day of coding. Thanks for the response.
            – Steve Coleman
            Dec 9 at 16:33
















          mythz I am using 5.4.1 and I get the error. 'TableOptions' does not contain a definition for 'Select' and the best extension method overload 'OrmLiteReadApi.Select<Company>(IDbConnection, string)' requires a receiver of type 'IDbConnection'
          – Steve Coleman
          Dec 7 at 23:04






          mythz I am using 5.4.1 and I get the error. 'TableOptions' does not contain a definition for 'Select' and the best extension method overload 'OrmLiteReadApi.Select<Company>(IDbConnection, string)' requires a receiver of type 'IDbConnection'
          – Steve Coleman
          Dec 7 at 23:04














          @SteveColeman sounds like your code is wrong, db.TableAlias() is only an argument and .Select() should only be called on the SqlExpression not TableAlias(). You can check out the commit for more usage examples.
          – mythz
          Dec 8 at 2:44




          @SteveColeman sounds like your code is wrong, db.TableAlias() is only an argument and .Select() should only be called on the SqlExpression not TableAlias(). You can check out the commit for more usage examples.
          – mythz
          Dec 8 at 2:44












          :-( ... I was missing a ) It was a long day of coding. Thanks for the response.
          – Steve Coleman
          Dec 9 at 16:33




          :-( ... I was missing a ) It was a long day of coding. Thanks for the response.
          – Steve Coleman
          Dec 9 at 16:33


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436421%2fjoining-same-table-multiple-times-in-servicestack-ormlite%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo