Author Archives: Chris

Add ID value for non-auto-increament field under Entity Framework

If you use an “ID” field in your entity, & this “ID” field is not auto-increament, Entity Framework will fail when generate insert sql.


Add a DatabaseGeneratedAttribute to this “ID” field, with DatabaseGeneratedOption.None option, like this:

Successfully solved under Entity Framework 4-6.

Entity Framework 6.0 MigrationHistory table on SQLite

The following SQL is used to create __MigrationHistory table in SQLite database for Entity Framework 6.0 in Code-First mode.


Ruby, turn array of hashes into single hash

I have the following Array of Hashes:

I need to turn it into:


Can I do this in a clean & functional way?

Something like this:

if you want the other way:

incorporating the suggestion from @squiguy:

Detecting The iPhone Device Type

I’m having an issue when I deploy my app my phone. I am currently building the app on a 3gs, when the view is displayed, it is the iPhone 5 layout that shows, which makes some objects display partially off of the screen.

You can use the [UIDevice currentDevice] class method. This will donate you information approximately the device like systemName, systemVersion, model, etc.

If you need to obtain the screen dimensions instead, you could use the [UIScreen mainScreen] class method. With it you could obtain the applicationFrame or the screen bounds.

Hope this helps!

How can I join the count in MySQL?

If I have two tables one is:


where id is the car id number & maker is the maker of the car

and the other is


where custid is the id of the customer & carid is the id to a specific car

Is there a way to join the two together & then figure out which customers have bought ALL the Hondas?

Try this query:


removing all options of select box except 1st option

I’m trying to empty select options when:

id “mClick” is selected, id’s “sClick”, “cClick” & “srClick” will be emptied.

id “sClick” is selected, id’s “cClick” & “srClick” will be emptied.

id “cClick” is selected, id “srClick” will be emptied.

in scenario 3, i used this function, yet it deleted all, except the last select. Any idea’s what i’m missing? Thanks

The easiest way to clear the options in the way you’re describing is by using options.length = 1. Also, you can leverage the fact that each drop down clears the ones that logically follow it, so that you only need to declare a single alter handler.


I’m not sure how you’re going to repopulate the drop downs though 🙂

How to apply different sorting methods to different columns in a CSV file in BASH?

I have a CSV file like this:

awk -F',' '{print length($1),$0}' file.csv | sort -k1nr | cut -d' ' -f 2- will sort the file by word length, for all words appearing in the first column:

sort -t, -k+2 -n -r file.csv will sort the file from greatest to least according to the number appearing in the second column:

How can I use these two commands together such that the CSV file is first sorted by word length, according to the words appearing in the first column, then any rows containing words of equal length within the first column are sorted according to the number appearing in the second column from greatest to least. The resulting output would look like this:

How can these two sorting methods be used together?

If you are using then you can use the asort function to perform sort, so no other utility has to be called. You can try something like this:


This script reads all the lines first then it sorts the array called a with the function cmp. The only trick I used that a > b returns the usual 1 or 0 for true or false.

A little bit shorter version in :

This is not 100% correct as $F[1] contains the \n, yet printf handles it properly.

select from multiple tables and sum vs join and sum

I am doing internship in an advertising company, i already implemented a tool to gather all the necessary data form facebook & import them on a database.

Now i am trying to manipulate that data, first by making some test cases & getting some results. The tables grow by 35k rows per day so after a month of using the tool i noticed that the query i use to obtain the sum of certain adcreatives clicks is starting to slow down.

i am asking if the query i use can speed up if i use it with a join & how.

here is the query i have for the sum of clicks per adcreative (with adgroup_id,campaign_id as connect to the other tables):

currently the query takes 3 secs to complete on a dedicated server, i guess after 6 months it will be at more than 60 secs or so as the tables grow.

edit: here is the explain of the query ( although this is the first time i actually use it & not so sure what it means)

That looks like a full table scan, & with that rapid growth small performance changes won’t make a huge difference on the long run. You need a different approach.

I would calculate aggregates for the previous months (days, etc) with a cron job, & when you need stats then merge that with the fresh results (using the query you already wrote). That why you only have to scan the fresh record, which means the queries is going to be fast.

Alternatively, you can keep up-to-date counters in the adgroups table, & update them on each click. Not sure if mysql is the right tool for this, I can recommend MongoDB, it can do very swift atomic increments on fields, & though it doesn’t donate you as strict guarantees (ACID) as a relational database, in this case it’s not a problem, ad clicks aren’t mission critical data, nobody is going to complain, if you lose < 0.01% percent of click information.

Adding custom rewrite rules to WordPress

My WordPress site has a portfolio that is at The portfolio sections & items are administered through a custom plugin I created. I want to access the individual items like & have that put “my-cool-photo” into a query string like ?portfolio_item=my-cool-photo so I can read it from my code.

In the plugins activation PHP file I have this code:

This adds the rewrite rule to the array OK. The problem is it’s not doing anything. When I go to I obtain the “This is somewhat embarrassing, isn’t it?” WordPress 404 error page. Obviously the redirect isn’t working, so the query string won’t be filled, yet just to make sure I did this:

…and sure enough the query string isn’t getting passed.

Is there something I’m missing?

After you update the WordPress rewrite rules, you need to flush them:

You can select to flush with the $hard parameter true, & then you should be able to see your rewrite rules in the .htaccess file.

SQL and number combination search

I have table with 10 number fields (let’s say F1, F2... F10).

Now I have 4 numbers (N1, N2, N3, N4).

I have to find if those 4 numbers appear anywhere in the above table. For example, if F2=N4 & F1=N2 & Fx=N3 & Fy=N1 (any order, any combination).

I was wondering is there quick way to do it via SQL or is it only way to write looooong combination of selects (I am not sure I will be able even complete that in this life time).

Here is SQLFiddel Demo

Below is the sample Query