SubSonic.SqlQuery incorrectly generated when using Where() - subsonic

I am using version 2.1 of SubSonic. I am trying to build to build a relatively simple query where I get a list of Roles for a User using UserId as a parameter. (UserId is a string...)
SubSonic.SqlQuery sel = new SubSonic.Select().From(Data.Tables.Role).InnerJoin(Data.Tables.UserRole, Data.UserRole.Columns.RoleId, Data.Tables.Role, Data.Role.Columns.Id).InnerJoin(Data.Tables.User, Data.User.Columns.Id, Data.Tables.UserRole, Data.UserRole.Columns.UserId).Where("[dbo].[User].[Id]").IsEqualTo(userId);
this generates the query
SELECT [dbo].[Role].[Id], [dbo].[Role].[PID], [dbo].[Role].[Name]
FROM [dbo].[Role]
INNER JOIN [dbo].[UserRole] ON [dbo].[Role].[Id] = [dbo].[UserRole].[RoleId]
INNER JOIN [dbo].[User] ON [dbo].[UserRole].[UserId] = [dbo].[User].[Id]
WHERE [dbo].[User].[Id] = #[dbo].[User].[Id]0
which fails. If I replace the Where with .Where(Data.User.Columns.Id) this generates the query
SELECT [dbo].[Role].[Id], [dbo].[Role].[PID], [dbo].[Role].[Name]
FROM [dbo].[Role]
INNER JOIN [dbo].[UserRole] ON [dbo].[Role].[Id] = [dbo].[UserRole].[RoleId]
INNER JOIN [dbo].[User] ON [dbo].[UserRole].[UserId] = [dbo].[User].[Id]
WHERE [dbo].[Role].[Id] = #Id0
which uses the Role table in the Where clause instead of the User table.
Is this a bug, or am I doing something incorrectly? What would be the correct way to do this? Thanks.

This is fixed in version 2.2 - I would suggest upgrading.

Related

Fluent BQL producing unexpected SQL for join

I'm using Fluent BQL to join a CreatedByID column to Users, to get the user name (simple example below).
public SelectFrom<GIDesign>
.InnerJoin<CreatedUsers>.On<CreatedUsers.pKID.IsEqual<GIDesign.createdByID>>
.OrderBy<GIDesign.name.Asc>.View GIs;
However, the SQL join condition ends up like this...
INNER JOIN [Users] [CreatedUsers_Users] ON NULL = [GIDesign].[CreatedByID]
AND ( [CreatedUsers_Users].[CompanyID] IN ( 1, 2)
AND 8 = SUBSTRING( [CreatedUsers_Users].[CompanyMask], 1, 1) & 8)
AND [CreatedUsers_Users].[DeletedDatabaseRecord] = 0
The join condition NULL = CreatedByID is of course returning no records. I tried swapping the left and right side of the join and then get this as the first join condition:
INNER JOIN [Users] [CreatedUsers_Users] ON [GIDesign].[CreatedByID] =
AND ...
Notice the missing right side of the equation? What am I missing, or doing wrong?
I was able to solve it myself shortly after I posted this. I removed the PXProjection attributes, added a Serializable attribute, plus removed PK member classes I had added to try to resolve the issue. So one of these changes must have been the key.

How to add AND condition when use inner join on Sequelize

I have some sql code like this
select *
from people_work_attendances pwa
inner join people_work_placements pwp on pwa.user_work_id = pwp.id
inner join company_job_profile_r_attendance cjpa on pwp.job_profile = cjpa.profile and pwa.policy_id=cjpa.policy
WHERE
pwa.user_work_id = 71072
and cjpa.profile IS NOT NULL
i want to implement this section to Sequelize
inner join company_job_profile_r_attendance cjpa on pwp.job_profile = cjpa.profile and pwa.policy_id=cjpa.policy
current situation is i can make it like this
if(!module.exports.Placement.associations.JobProfileAttendance){
module.exports.Placement.hasOne(module.exports.JobProfileAttendance, {
as:'JobProfileAttendance',
sourceKey:'jobProfile',
foreignKey:'profile'
})
}
but the result of this part is like this
INNER JOIN "public"."company_job_profile_r_attendance" AS "Placement->JobProfileAttendance" ON "Placement"."job_profile" = "Placement->JobProfileAttendance"."profile"
i want to make like this
INNER JOIN "public"."company_job_profile_r_attendance" AS "Placement->JobProfileAttendance"
ON "Placement"."job_profile" = "Placement->JobProfileAttendance"."profile"
AND "PeopleWorkAttendance"."policy_id" = "Placement->JobProfileAttendance"."policy"
want to add some AND conditions but in Sequelize
AND "PeopleWorkAttendance"."policy_id" = "Placement->JobProfileAttendance"."policy"
You can use on option in include, just keep in mind that you always should do it manually because Sequelize doesn't support composite primary and foreign keys.
See options.include[].on in findAll options

How to get a SELECT DISTINCT on a SelectMulti query in ServiceStack OrmLite?

I'm trying to get a distinct result set of tuples, but the Distinct never gets added to query.
Example
List<Tuple<Alpha, Beta>> results;
var q = dbConn.From<Alpha>()
.Join<Alpha, Beta>((a, b) => a.Id == b.AlphaId)
...
... more joins and Wheres
...
.SelectDistinct();
results = dbConn.SelectMulti<Alpha, Beta>(q);
Adding the SelectDistinct, or not, make no difference to the outputted SQL and hence results.
How do I get SelectMulti to work with Distinct?
Thanks.
I've just added support for this in this commit where if .SelectDistinct() is used in the SqlExpression<T> then it will execute the SQL query using SELECT DISTINCT, e.g:
var results = dbConn.SelectMulti<Alpha, Beta>(q.SelectDistinct());
This change is available from v5.4.1 that's now available on MyGet.

subsonic join to nested selected statement

Is it possible to do a join to a nested select statement in SubSonic 2.2? I am having trouble with this because I have to do a group by...
I.E.
select conn_company.company_name, SUM(t.quote_grand_total) as 'SUM' from conn_company
inner join
(SELECT *
FROM [dbo].[QuoteDBAll]
where [dbo].[QuoteDBAll].[quote_status_id] = 1
AND [dbo].[QuoteDBAll].[quote_ca_modified_date] BETWEEN '12/1/2000' AND '12/1/2009'
) t
on conn_company.company_id = t.company_id
group by company_name
having company_name = 'JACK'
Unfortunately you're not going to be able to convert that SQL into SubSonic 2 syntax. I would suggest you create a sql view based on your query and get SubSonic to generate a model from that instead.

Subsonic 3 Simple Query inner join sql syntax

I want to perform a simple join on two tables (BusinessUnit and UserBusinessUnit), so I can get a list of all BusinessUnits allocated to a given user.
The first attempt works, but there's no override of Select which allows me to restrict the columns returned (I get all columns from both tables):
var db = new KensDB();
SqlQuery query = db.Select
.From<BusinessUnit>()
.InnerJoin<UserBusinessUnit>( BusinessUnitTable.IdColumn, UserBusinessUnitTable.BusinessUnitIdColumn )
.Where( BusinessUnitTable.RecordStatusColumn ).IsEqualTo( 1 )
.And( UserBusinessUnitTable.UserIdColumn ).IsEqualTo( userId );
The second attept allows the column name restriction, but the generated sql contains pluralised table names (?)
SqlQuery query = new Select( new string[] { BusinessUnitTable.IdColumn, BusinessUnitTable.NameColumn } )
.From<BusinessUnit>()
.InnerJoin<UserBusinessUnit>( BusinessUnitTable.IdColumn, UserBusinessUnitTable.BusinessUnitIdColumn )
.Where( BusinessUnitTable.RecordStatusColumn ).IsEqualTo( 1 )
.And( UserBusinessUnitTable.UserIdColumn ).IsEqualTo( userId );
Produces...
SELECT [BusinessUnits].[Id], [BusinessUnits].[Name]
FROM [BusinessUnits]
INNER JOIN [UserBusinessUnits]
ON [BusinessUnits].[Id] = [UserBusinessUnits].[BusinessUnitId]
WHERE [BusinessUnits].[RecordStatus] = #0
AND [UserBusinessUnits].[UserId] = #1
So, two questions:
- How do I restrict the columns returned in method 1?
- Why does method 2 pluralise the column names in the generated SQL (and can I get round this?)
I'm using 3.0.0.3...
So far my experience with 3.0.0.3 suggests that this is not possible yet with the query tool, although it is with version 2.
I think the preferred method (so far) with version 3 is to use a linq query with something like:
var busUnits = from b in BusinessUnit.All()
join u in UserBusinessUnit.All() on b.Id equals u.BusinessUnitId
select b;
I ran into the pluralized table names myself, but it was because I'd only re-run one template after making schema changes.
Once I re-ran all the templates, the plural table names went away.
Try re-running all 4 templates and see if that solves it for you.

Resources