Leaving Echolibre, Joining PNC Bank

I am very sad to be announcing that Friday, June 11th was my last day working for Echolibre. In the few short months that I spent with Echolibre I learned so much, and I am proud of everything I accomplished there. The people at Echolibre are second to none, and I am happy to add Eamon and David to the list of incredible bosses that I've been fortunate to have.

Despite my sadness to be leaving Echolibre, I am also proud to announce that I will be taking a position with PNC Bank.

At PNC I will be working in the marketing department, primarily on front-end development. I look forward to the opportunity to develop my CSS and Javascript skills in this role. This may come as a surprise to those that know me well, since I have always expressed a strong preference for back-end development. Over the years I've developed a laundry list of software projects that I am eager to work on; unfortunately, working with PHP and Python full time has taken its toll and I've found myself neglecting these side projects because I'm simply exhausted of thinking about the same kinds of problems all day. Furthermore, I have identified several areas that I want to further study so that my knowledge of programming can be more complete and more useful in future endeavors. I hope that by focusing on an entirely different type of challenges during my day job I will be able to better concentrate on my side projects and continued learning after hours.

PNC presents some new challenges that will take a lot of getting used to. For the first time in my professional career I will be working in an office on a daily basis from 8 AM to 5 PM, all the while adhering to a company dress policy. There is a small (very small) part of me that looks forward to getting on a more strict daily schedule, particularly since my sleep schedule has slowly gone to shit over the last few years.

Every job change is a catalyst for change in other areas of my life, and I'm looking forward to coming out of this transition as a new and improved Slango. I don't expect the transition will be an easy one, but I look forward to what the future holds. I've already set some goals for myself in the coming weeks and months. The first of those will be to blog more. I've recently been experimenting with CouchDB, Python, Javascript, jQuery, and Pylons, and I hope I will more frequently make time to share my experiences with you all.

Thanks for reading. Now, it is about bed time; only 15 hours left to sleep before I have to be downtown for my first day of work.

Django: My Initial Thoughts

I've been unemployed since October and while I look for my next opportunity, I've been utilizing some of my spare time trying to improve my professional skills. One of the areas I've been most focused on is learning Python, and along with it, the very popular Django web application framework. Stepping away from PHP and into the Python domain is like stepping into a whole new world. Python's syntax isn't too hard to catch on to, but things are just done differently in the Python world. One thing that really struck me about Python is the amount of extra work involved in building a simple web application, and the many paths available to go about it. Compared to Python, PHP basically has a web framework built right in. All of the things that we take for granted in the PHP world require a decision at the very least in Python.

Enter Django. I wanted to start making a web application, and I wanted to see results now. The most logical path was to pick a framework to do the heavy lifting for me. Django brought along with it more weirdness, but also some comfort. First of all, Django is built on an MVC architecture (though no one associated with Django will ever admit that), which immediately added some familiarity. Coupled with a simple model API, the ball was really starting to get rolling. I've always believed that one cannot really learn a language without actually building something that interests them. With Django helping me write my first Python web application, I was on my way to learning Python.

The first thing that interested me was Django's URL configuration. Django takes regular expressions which URLs are compared against. Each expression is paired with a callback function, and the callback function that is associated with the first expression that matches the request URL is called. My friend Chris Shiflett is always going on about how important attractive URLs are, and here is a framework that actually forces the developer to consider URLs before anything will work. I have never seen URLs given such royal treatment by a web framework, and this is exactly the type of thing that I like about Django. As a matter of fact, the Python manifesto asserts that explicit is better than implicit, and that statement almost certainly lead Django's developers to handling URLs in this way.

Let's contrast the URL configuration with something that I do not care for much at all about Django. Django lacks support for composite foreign and primary keys. Certainly composite keys aren't the most often used feature of SQL, but if you read my last blog entry you can see that they do have their place. What is more frustrating is that the existing implementation of primary and foreign keys was seemingly conceived of without even the slightest consideration for future support of composite keys. Primary and foreign keys for a model are referenced everywhere in Django, and none of the existing code is conducive to being adapted for composite keys, making it very difficult to develop a feasible solution while maintaining backward compatibility.

Django makes up for this huge shortcoming with another killer feature: manage.py. A manage.py script is automatically created when you create a new Django project. This script is a collection of great tools for your project. With it you can do all of the usual stuff that you can do with Cake's command line tool, for example. Django takes it to a new level by offering the `shell` command. When you execute `python manage.py shell`, Django launches the excellent Python interactive shell, but it goes a step further. Django bootstraps the Python shell for your given project, making it easy to quickly test some code or play with the Django API. Django also offers a built-in development webserver, via the `runserver` argument to manage.py. I have never been a fan of command-line tools associated with web frameworks, but Django's adds undeniable value to an already powerful web application framework.

These are just my initial thoughts. There is a lot of Django left to learn, and I expect that I will write about it again in the future. For now, I encourage you to give Django a shot for your self. I'd love to hear your experiences with Django in the comments.

A Many-to-Many Relationship with a Shared Foreign Key

A Tricky Problem

We are going to create a simple database which stores states, counties and cities and the links between them. You might expect to design a database in which there is a Cities table, which has a foreign key linking it to the Counties table, which has a foreign key linking it to the States table. This is a pretty reasonable schema. Each county may contain many cities, but each city may only be in one county. Each state may contain many counties, but each county may only be in one state. Perfect you say? Not exactly.

In our great country, we allow all sorts of freedoms. One such freedom is the right for government officials to screw up simple and basic organizational heirarchies. While it is true that most cities are contained within a single county, there are actually many that are not. One very notable example is New York, NY, which spans the counties of Queens, Kings, Bronx, Richmond, and New York.

Back to the drawing board then. We need a design that supports a many-to-many relationship between the Counties table and the Cities table. More importantly every city and every county belongs to a single state, so every Cities or Counties record ought to link to exactly one States record. So far, so good.

The trouble starts when we consider that for any pair of counties and cities, both the county and city must belong to the same state. Sure, it's trivial for our application to verify that Counties.state_id is the same as Cities.state_id on insert, but I'm the kind of guy that likes to deal in certainties whenever I can, and in software you can't get much more certain than a database constraint.

Solving the Puzzle

Now that we've got a pretty good grasp on the problem, let's take a look at how to fix it.

A diagram showing states, counties and cities.

Above is a diagram illustrating the relationships between the tables. Below this paragraph is the SQL DDL for the table, with the concepts we've discussed applied.


CREATE TABLE States (
    id VARCHAR(2) PRIMARY KEY,
    name VARCHAR(15)
);
CREATE TABLE Counties (
    id INTEGER AUTOINCREMENT,
    state_id VARCHAR(2) REFERENCES States (id),
    name VARCHAR(20),
    PRIMARY KEY (id, state_id)
);
CREATE TABLE Cities (
    id INTEGER AUTOINCREMENT,
    state_id VARCHAR(2) REFERENCES States (id),
    name VARCHAR(30),
    PRIMARY KEY (id, state_id)
);
CREATE TABLE States_Counties_Cities (
    state_id VARCHAR(2) REFERENCES States (id),
    county_id INTEGER,
    city_id INTEGER,
    PRIMARY KEY (state_id, county_id, city_id),
    FOREIGN KEY (state_id, county_id) REFERENCES Counties (state_id, id),
    FOREIGN KEY (state_id, city_id) REFERENCES Cities (state_id, id)
);

Because we make the primary key of the Muncipalities and Counties tables include the state_id, we are forced to include it in our reference to them from the States_Counties_Cities table. The trick here is to only include one state_id column, so that it has to serve as the state_id portion of the foreign key to both Cities and Counties. Since we're only using one column, we cannot possibly join a muncipality in one state with a county in another state. Our problem is solved in such a way that we can rest assured that our database's integrity will be preserved.

A Final Thought

I came up with this schema while considering a project that I am working on for a friend's martial arts school. The actual context that I am using this concept in for that project is more complex than this and was difficult to explain to people. I hope this entry has been easy to understand and gives you a chance to think about how a similar concept might be applied to data challenges which you are facing now, or have faced in the past. I had great help in figuring this out from Robert Treat, Alejandro Garcia, and Twist from the #sql channel on Freenode. In fact, the #sql, #sqlite and #postgresql channels on the Freenode IRC network are very helpful places to get answers to any crazy database problem you might dream up.

If you come up with a creative way to apply this, or an interesting problem that it helps solve, please let me know in the comments. Oh, and thanks for reading!

PS: Some early readers reported that the abstract discussion of this concept was muddying the waters a bit in terms of clarity. I've moved that paragraph below, so if you're interested in my abstraction of this whole mess, read on. Otherwise, thanks again for reading!

In Technical Terms…

Table B and table C in the diagram below both have a many-to-one relationship with table A. They also enjoy a many-to-many relationship with eachother through table D. The record in table A that is referenced by table B has to be the same record in table A that is referenced by table C. In other words, table D not only has to join B and C, but also make sure they are talking about the same record from table A. Yikes! A tall order.

An abstract diagram.

The above diagram illustrates a set of tables A, B, C and D which have the following constraints:

Table A:
  • Primary Key: a
  • Foreign Keys: None
Table B:
  • Primary Key: (a, b)
  • Foreign Keys: A.a
Table C:
  • Primary Key: (a, c)
  • Foreign Keys: A.a
Table D:
  • Primary Key: (a, b, c)
  • Foreign Keys: A.a, (B.a, B.b), (C.a, C.c)

Because the foreign-key to table A is used as a part of the primary key for tables B and C, we must use it to reference tables B and C in table D. The caveat is that instead making an aB column and a separate aC column on table D, we simply create one a column, which is used in the foreign key for table B and table C. This forces the joined records from B and C to share the same foreign key value for table A, and thus is the solution to our problem.

Welcome to Pittsburgh!

The previous iteration of this blog sat dormant for months because I couldn't think of anything technology related to blog about. Yes, I want to be an inspiring PHP blogger like most of my co-workers and friends in the community. But there is a lot more to me than just PHP.

Did you know that in July, I moved to Pittsburgh? I bet at least one person will read this that didn't know that. My boss and co-workers keep trying to convince me to abandon Pittsburgh for New York City. I won't make any attempt at denying that NYC is a truly awesome place; but right now, Pittsburgh is the place for me. I thought I would spend a few minutes to reflect on some of the things that I love about this city, in hopes of showing others exactly what makes this city so awesome.

First and foremost, I love the academic community surrounding Pittsburgh. The University of Pittsburgh was ranked 13th among US public schools by US News & World Report. The prestigious Carnegie Mellon University was ranked even higher on the same list. Pittsburgh is also home to Duquesne University and Carlow University and many smaller colleges and trade schools. That means there are a lot of students and educators all crammed together in one little city, sharing thoughts and ideas, and working on some really cool things. Being surrounded by so many smart people every day instills a sense of pride and inspiration, which is not unlike the feeling of working at OmniTI.

I think this often goes hand in hand with a strong academic community, but it's worth pointing out Pittsburgh's excellent and growing art community. I've yet to enjoy most of the art that can be found within Pittsburgh's borders, which ranges from museums to film to music and theater (I'll blog about examples of these as I experience them). And that is only the tip of the iceberg! There is a lot more to be seen, heard, and experienced in Pittsburgh. Maybe not as much as in NYC, but certainly plenty to keep a busy graduate student/PHP professional busy.

One of the great things about New York City is that there are hundreds, if not thousands of neighborhoods within the five boroughs, each with it's own character and charm. While Pittsburgh doesn't have quite as many neighborhoods as NYC, they're all a lot closer together. Every time I get in my car, I end up finding a new restaurant or shopping district that looks enticing. I bookmark these locations and can come back easily for a visit. The diverse character of certain areas means that there is never nothing to do in Pittsburgh.

Now, I wouldn't be honest if I let you believe that Pittsburgh is a modern day Eden. There are a few areas in which Pittsburgh could do a little bit better. The most glaring trouble with my beloved city is its hours of operation. Many shops and restaurants keep short hours, which may not include evenings, Saturdays, Sundays, and in some cases, even Mondays. It's terribly depressing to head out for a sushi lunch in the middle of a Sunday afternoon only to find that Oishii is closed. Another gripe of mine, which is probably irreparable at this point, is Pittsburgh's public transportation. The bus system is much better than Detroit's mockery of a bus system, but after spending three weeks in New York City, I've grown to love the subway system. I find it hard to believe that there is better public transportation anywhere in the world.

So that is my first post about Pittsburgh. I'd like to get into detail about interesting things as I find them, so expect many more entries regarding Pittsburgh to follow.

Chris Shiflettt pointed out that I invalidate my own argument about Pittsburgh having a lot of neighborhoods that are close together when I mention driving around between them. That is mostly my detest of winter weather talking. It's worth noting that I can easily walk to a few different neighborhoods from my apartment. The bicyclists among us have even more options, and a short bus ride opens up every Pittsburgh neighborhood.

Switch to Habari

I've just made the switch to Habari as my blogging platform of choice. I was previously using Madoqua, which is based on the Zend Framework and written by Naneau. I still fully support that project; I keep an updated copy on my local system, and hope to continue helping to spot bugs, and even contribute.

My decision to switch to Habari was fueled by the fact that, while Madoqua is coming along nicely, it is still late alpha, or early beta software. I expect development to continue, and I believe that we will see a truly unique and powerful blogging platform emerge from the efforts of the Madoqua developers. In the end, Habari will be no permanent solution either. I have some thoughts on exactly how I would like to use my blog, thoughts which were in part inspired by a discussion with Sean Coates, as well as my personal reflections on what I like about many blogs that I've seen and read. In the end, I hope to develop a solution that fits me. If this project comes to fruition, I fully expect to share the source as my first public post on that blog.

Welcome! I hope to use this blog a lot more, and you should expect a post soon regarding PhD: The PHP DocBook Renderer which is responsible for all of the great PHP documentation that you use everyday. Anyway, please leave comments. It would be nice to know that someone is actually reading this.

 1

About

User