Selecting from a View in Subsonic causes an error! - subsonic

Sorry if this seems a silly questions but can you select from a view in subsonic?
I have a view called fixturesinfo and I am running this subsonic query:
FixturesInfoCollection fixtures = new SubSonic.Select().From<FixturesInfo>()
.Where(FixturesInfo.Columns.FixtureDate).IsGreaterThan(DateTime.Now.AddMonths(-3))
.ExecuteAsCollection<FixturesInfoCollection>();
When I run it I get an error dbo.FixturesInfo.
This happens with all views.
Am I doing something wrong here?
Thanks Bex

Worked it out..
I am using two database connection string and it appears as it was a generic select it wasn't picking the right one.
Instead if I do this:
FixturesInfoCollection fixtures =new FixturesInfoCollection()
.Where(FixturesInfo.Columns.FixtureDate,
Comparison.GreaterThan, DateTime.Now)
.OrderByAsc(FixturesInfo.Columns.FixtureDate).Load();
It works

Related

Sequelize Tables (Beginner Question) Node.js

Ok so I just started looking into databases yesterday
index.js: https://hatebin.com/pvqnubsrrs
database.js: https://hatebin.com/csgvmvolfz
I just want it to insert into the DB,
how would I accomplish that?
Someone told me it's creating a new DB, but when I run the command it says the table doesn't exist
The error says:
Error: SequelizeDatabaseError: SQLITE_ERROR: no such table: PointsSystems
However, I cannot find a spot in the code where I use PointsSystems, I only use PointsSystem, without the s
Edit: Thank you SigFried for making this more readable.
The error says PointSystems because, by default, Sequelize will pluralize the name you gave to the table. Your code looks O.K, the only missing part and the solution to your problem is the await sequelize.sync({ force: true })}. I made a little example to show you how it's done in a very basic way.
You should be able to find out why the sequelize.sync() methods is needed if you read this.

CoreData with #FetchRequest (SwiftUI) and NSPredicate crashes if there is no data

I have a SwiftUI app where I am using #FetchRequest along with a predicate. Everything works fine as long as there is already some data.
However, when the app is first installed and the user tries to perform a search before entering any data, the app crashes with this error:
error: SQLCore dispatchRequest: exception handling request: <NSSQLFetchRequestContext: 0x60000188c380> , unimplemented SQL generation for predicate : (username CONTAINS[cd] "r") with userInfo of (null)
I believe the cause of the problem is that the column doesn't exist (because there is no data). What I'd like to do is pass nil for the predicate in the case where this is no data.
I understand how to use NSManagedObjectContext and count(for:) but the context isn't really made available via the #EnvironmentObject at the time I need to use it. Does anyone have any suggestions as to how to handle this. I don't see how a try-catch would work either.
Thanks.
Well, I do have a solution. Its not pretty, but it does work. Because the NSManagedObjectContext is being passed around via #Environment it isn't initialized when a SwiftUI View init is being run. So to get the context you can do:
if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext {
and make the call to get the count in there.
So I'm clueless sometimes. I did not read the error thoroughly since it happened in the context of something I was changing so I thought the problem was that. But no, that is not the problem.
The problem is that earlier in the day, I renamed a column from username to something else. But I forgot to change the NSPredicate.
So for the sake of anyone else using SwiftUI and #FetchRequest, if you do need to count the number of items, you will need to revert to NSFetchRequest to do it. Sorry for wasting anyone's time.

Kohana ORM Result - How to display?

Maybe something really simple, I got really big problem with... ORM result.
I'm loading object with relation using with(). It generates following query:
SELECT `article`.`id` AS `article:id`, `article`.`name` AS `article:name`
Now's my question... how to display article name in the view? Sorry for dumb question, I really can't beliebe I'm asking it.
Edit
Here's my code:
$activity = $user->activity->with('article')->where('article.status', '=', 1)->find_all()->as_array();
Relations are correct for sure. I can swear I saw something similar today morning on the Kohana Forums however cannot find it.
Cheers!
I haven't tested it but does it work if you do this:
$activities = $user->activity->with('article')->where('article.status', '=', 1)->find_all();
foreach($activities as $activity) {
echo $activity->name.'<br />';
}

Subsonic BatchQuery.Queue causing 'Can't decide which property to consider the key...' exception

I'm just getting started with Subsonic 3.0 ActiveRecord and am trying to implement a batch query like the one in the SubSonic docs. I'm using a batch so I can query a User and a list of the users Orders in one shot.
When I call the BatchQuery.Queue() method, adding my "select user" query, SubSonic throws the following exception:
System.InvalidOperationException : Can't decide which property to consider the Key - you can create one called 'ID' or mark one with SubSonicPrimaryKey attribute
The code is as follows:
var db = new MyDB();
var userQuery = from u in db.Users //gets user by uid
where u.uid == 1
select u;
var provider = ProviderFactory.GetProvider();
var batch = new BatchQuery(provider);
batch.Queue(userQuery); //exception here
//create and add "select users orders" query here...
First things first - Why this error? My SubSonic Users object knows it's PK. "uid" is the PK in the database and the generated code reflects this. And I thought SubSonicPrimaryKey attribute was for the SimpleRepository? Is this way of batching not for ActiveRecord?
I could ask a number of other questions, but I'll leave it at that. If anyone can help me figure out what is going on and how to issue 2 batched queries I'd be grateful!
Edit - after further investigation
I ran through the source code with the debugger. Adam is correct - the ToSchemaTable() method in Objects.cs is apparently building out my schema and failing to find a PK. At the very end, it tries to find a column property named "ID" and flags this as the PK, otherwise it throws the exception. I added a check for "UID" and this works!
Still... I'm confused. I'm admittedly a bit lost after peeling back layer after layer of the source, but it seems like this portion of code is trying to build up a schema for my table and completely ignoring my generated User class - which quite nicely identifies which column/property is the PK! It doesn't seem quite right that I'd be required to name all keys "ID" w/ ActiveRecord.
I think the answer you're looking for is that this is a really stupid bug on my part. I'm hoping to push another build next week and if you could put this on the issue list I'd really appreciate it. My apologies...
SubSonic expects your primary key to be called Id so it's getting confused. SubSonicPrimaryKey is for simple repository but I assume where that exception is being thrown is shared between the different templates. If you rename your PK to Id or id or ID your query will work.

subsonic query problem

I'm using subsonic 2.2 in an app. I'm running a little complicated query in I have used both "And" and "Or" for a field, I'm little confused about how it is going to be translated into sql statement
MytableCollection col = DB.Select().From("mytable").Where("prop1").IsEqualTo(obj.prop1)
.And("prop2").IsEqualTo(obj.prop2)
.And("prop3").IsEqualTo(obj.prop3)
.Or("prop1").IsEqualTo(1)
.ExecuteAsCollection<MytableCollection>();
I want to perform query like this.
select * from mytable where (prop1=obj.prop1 or prop1=1) and prop2=obj.prop2 and prop23=obj.prop3
As Andra says you can use AndExpression. This should do what you want:
MytableCollection col = DB.Select().From(Mytable.Schema)
.Where(Mytable.Columns.Prop2).IsEqualTo(obj.prop2)
.And(Mytable.Columns.Prop3).IsEqualTo(obj.prop3)
.AndExpression(Mytable.Columns.Prop1).IsEqualTo(obj.prop1)
.Or(Mytable.Columns.Prop1).IsEqualTo(1)
.ExecuteAsCollection<MytableCollection>();
N.B. using MyTable.Schema and MyTable.Columns will catch a lot of issues at compile time if you rename tablees and will save errors caused by mistyping
Something that is REALLY useful to know about is also the following two methods to call in your query building:
.OpenExpression()
and
.CloseExpression()
Mix those bad buoys and you have a lot better control over knowing where things start and finish
you can use expression in subsonic 2.2.
MytableCollection col = new Select(Mytable.Schema)
.WhereExpression("prop1").IsEqualTo(obj.prop1).Or("prop1").IsEqualTo(1)
.AndExpression("prop2").IsEqualTo(obj.prop2)
.AndExpression("prop3").IsEqualTo(obj.prop3)
.ExecuteAsCollection<MytableCollection>();

Resources