Subsonic 3 Guid as PK always gives me 00000000-0000-0000-0000-000000000000 - subsonic3

Using Subsonic 3, Simple Repository the Add always gives me a 00000000-0000-0000-0000-000000000000 for my PK which obviously causes an error in my DB. I see that the definition of my PK created by Subsonic is using newid() but still having this issue.
Anyone else having this problem?
FYI I am using a local SQL Server 2005 DB sitting in my App_Data directory.

When you use Guid's SubSonic expects you to set them. Your best bet is to Guid.NewGuid() in the constructor. Remember that the SimpleRepo is "POCO-first" thinking. The DB is just a place for data.

I'm also using a GUID as a PK. My PK default setting is...
(CONVERT([uniqueidentifier],CONVERT([binary](6),getdate(),(0))+CONVERT([binary](10),newid(),(0)),(0)))
I've been going through the subsonic source and I'm beginning to think that this is related to the fact that... shiz I don't quite know.
I do know that Subsonic isn't seeing the NOT NULL, PK, Default Setting having column as READ ONLY.
So, assuming you don't have any other PK datatypes in the solution, you could add a few lines to the Structs.tt file...
Find the foreach loop on line 30...
<# foreach(var col in tbl.Columns){#>
Columns.Add(new DatabaseColumn("<#=col.Name#>", this)
{
IsPrimaryKey = <#=col.IsPK.ToString().ToLower()#>,
DataType = DbType.<#=col.DbType.ToString()#>,
IsNullable = <#=col.IsNullable.ToString().ToLower()#>,
AutoIncrement = <#=col.AutoIncrement.ToString().ToLower()#>,
IsForeignKey = <#=col.IsForeignKey.ToString().ToLower()#>
<# if (col.IsPK.ToString().ToLower() == "true"){ #>
, IsReadOnly = <#=col.IsPK.ToString().ToLower()#>
<# } #>
});
The section you're adding is the IF statement just after IsForeignKey.
This should make it work for your your PK Guids so long as you have something in default setting.
I can't make any guarantees about any other PK's you may have in the DB though. You've been warn'd.

Related

Is there a way to add "Collation" in to Django 1.3 query?

I need to make a get query like:
obj = Current.objects.get(Code='M01.C0001')
But the query giving "Multiple Objects Returned' error because of the database has another record with similar unicode string 'M01.Ç0001'
[<obj: M01.Ç0001>, <obj: M01.C0001>]
I try to fetch data with field lookup functions, but it does not work anyway.
I googled around but I didn't find a way to temporarily set the Collation for this query.
Is it possible to temporarily set collation during executing a get query in Django 1.3?
SOLUTION:
I solved my problem with using raw django query with adding COLLATE to sql string.
obj = Current.objects.raw("SELECT * FROM Current WHERE Code = 'M01.C0001' COLLATE utf8_bin;")
Collation is a database property, so you cannot do that.
Change collation to database.

How do you correctly return an aggregate data field using AX 2012 Query Service

I have been working on the AX Query Service as of late. I have a pretty good understanding of everything but it seems that the QueryDataFieldMetadata object does not like aggregates. When I build a QueryDataFieldMetadata object:
QueryDataFieldMetadata field = new QueryDataFieldMetadata();
field.TableName = "InventSum";
field.FieldName = "AvailPhysical";
field.SelectionField = SelectionField.Database;
And add it to the data source everything is fine. But when I do this:
QueryDataFieldMetadata field = new QueryDataFieldMetadata();
field.TableName = "InventSum";
field.FieldName = "AvailPhysical";
field.SelectionField = SelectionField.Sum;
And add it to the data source the field is not returned at all in the results set. I have checked the datasource itself before executing the query and it is in the fields list but nothing is returned. Does anyone know why this might be happening? Any help would be appreciated.
I just figured this one out. The problem was due to me selecting another field from the table but forgetting to put it in the "Group by" fields. It is strange to me that the query service was returning THAT field with an empty but not returning the aggregate fields at all. Basically I had made a query service query that would be equal to this:
Select wMSLocationId, SUM(AvailPhysical), RecId from InventSum group by ItemId, InventLocationId, wMSlocationId where ItemId == 'some value';
The query was returning:
InventSum.wMSLocationId = 001
InventSum.RecId = 0
The inclusion of the RecId was a mistake, I had forgotten to remove it, but didn't think it would matter as it wasn't in the group by fields and would therefore return null. Removing this selection field did result in the aggregate field returning in the query.
Anyway I hope this helps someone out there as it took me some time to figure out.

how to match a field name with another field name

I have two fields that run throughout a website that I would like to match so that when a user inputs a value either of the fields, it will match the other field. I'm using Sitecore Rocks and am trying to use a query to do this.
select ##h1#, ##Title#
from /sitecore/Content/Home//*[##h1# !="##Title#"];
update set ##h1# = ##Title# from /sitecore/Content/Home//*[##Title# = "<id>"];
What am I missing here?
This article talks about tapping in to the item:saving event which allows you to compare the fields values of the item before and after the changes:
http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2010/11/Intercepting-Item-Updates-with-Sitecore.aspx
Using this, you can determine which field has been amended, then change the other to match.
I've had to do something similar to this when a new field was added, and we wanted to set the initial value equal to an existing field. It may be a bug in Sitecore Rocks, but I found it would only update a field when a static value was part of the query.
When I ran ##h1# = ##Title#, the query analyzer would return the correct number of items updated, but no values were actually updated. However, ##h1# = '<id>' worked perfectly. After trying a number of things, I found this did what I wanted.
update set ##h1# = '' + ##Title# from /sitecore/Content/Home//*[##Title# = "<id>"];
I hope that helps.

Sphinx Search powered app - wrong design?

I'm building web app and using django and Sphinx for free text search. I need to apply additional restrictions before making request to searchd, consider 2 tables:
Entity
id
title
description
created_by_id
updated_by_id
created_date
updated_date
and
EntityUser
id
entity_id [FK to the table above]
joining_user_id
is_approved
created_by_id
updated_by_id
created_date
updated_date
I've built RT index for main table Entity, all works fine, but then I want to make a query only on those entities to which user has joined, i.e. where for specific user_id & entity_id exists record in EntityUser with is_approved=1. Problem is that I can't index EntityUser, because there are no string fields - this table only holds integers/timestamps as you see. Not sure if I could make a query in SphinxQL containing subquery to another idex even if I could build index for that table. Knowing that Sphinx was used for quite big projects with great success, I doubt it's a limitation of Sphinx - is it bad design of DB/application or leak of knowledge how to build proper RT index? Can I somehow extend existing index so that I can use restriction above?
I was thinking that I could apply the additional restrictions after Sphinx returns IDs of records on MySQL side, but that's not going to work: N records with highest weight would be returned, but after applying additional restrictions the result could be empty. So I need to get an area of search and then perform query only on those entities user can possibly see.
Adapting the example from http://sphinxsearch.com/docs/current.html#attributes, you might be able to use something like this in your conf:
...
sql_query = SELECT app_entity.id as id,
app_entity.title as title,
app_entity.description as description,
app_entityuser.id as userid
FROM app_entity, app_entityuser
WHERE app_entity.id = app_entityuser.entity_id AND app_entityuser.is_approved = 1
sql_attr_uint = id
sql_attr_uint = userid
...
I should provide a disclaimer: I have not tried this.
I did find a related SO post, but it doesn't look like they quite solved it: Django-sphinx result filtering using attributes?
Good luck!
Actually I've found the answer and it has nothing to do with the design of application or DB.
In fact that's simple - I just need to use MVA for RT index as I would do for plain one (rt_attr_multi or rt_attr_multi_64). In configuration file I will have to do something like this:
...
rt_attr_multi = entity_users
}
and then populate it with IDs of users which have joined the Entity and have been approved. Problem was that I couldn't understand how to use MVA with RT index, but not it's clear. There are not enough real-word examples with RT indexes and MVA I think, so I've shared this to help to solve similar problems.
UPDATE: was fighting last hour to generate RT index and always was getting "unknown column: 'entity_users'". Finally found the reason - if you add MVA to RT index (don't know if that's the same for plain), you've got to not only restart searchd daemon (service), but also DELETE everything you have in "data" folder (or where you have stored your index)!

Django QuerySet access foreign key field directly, without forcing a join

Suppose you have a model Entry, with a field "author" pointing to another model Author. Suppose this field can be null.
If I run the following QuerySet:
Entry.objects.filter(author=X)
Where X is some value. Suppose in MySQL I have setup a compound index on Entry for some other column and author_id, ideally I'd like the SQL to just use "author_id" on the Entry model, so that it can use the compound index.
It turns out that Entry.objects.filter(author=5) would work, no join is done. But, if I say author=None, Django does a join with Author, then add to the Where clause Author.id IS NULL. So in this case, it can't use the compound index.
Is there a way to tell Django to just check the pk, and not follow the link?
The only way I know is to add an additional .extra(where=['author_id IS NULL']) to the QuerySet, but I was hoping some magic in .filter() would work.
Thanks.
(Sorry I was not clearer earlier about this, and thanks for the answers from lazerscience and Josh).
Does this not work as expected?
Entry.objects.filter(author=X.id)
You can either use a model or the model id in a foreign key filter. I can't check right yet if this executes a separate query, though I'd really hope it wouldn't.
If do as you described and do not use select_related() Django will not perform any join at all - no matter if you filter for the primary key of the related object or the related itself (which doesn't make any difference).
You can try:
print Entry.objects.(author=X).query
Assuming that the foreign key to Author has the name author_id, (if you didn't specify the name of the foreign key column for ForeignKey field, it should be NAME_id, if you specified the name, then check the model definition / your database schema),
Entry.objects.filter(author_id=value)
should work.
Second Attempt:
http://docs.djangoproject.com/en/dev/ref/models/querysets/#isnull
Maybe you can have a separate query, depending on whether X is null or not by having author__isnull?
Pretty late, but I just ran into this. I'm using Q objects to build up the query, so in my case this worked fine:
~Q(author_id__gt=0)
This generates sql like
NOT ("author_id" > 0 AND "author_id" IS NOT NULL)
You could probably solve the problem in this question by using
Entry.objects.exclude(author_id__gt=0)