Wednesday, August 3, 2011

Django 1.3 model ID not showing up after .save() with PostgreSQL

So there was an interesting change with how Django selects the value for an ID with the PostgreSQL back end between 1.1 and 1.3 (or perhaps 1.2, I'm not sure.)

The function to get the ID of the newly saved model used to read:

def last_insert_id(self, cursor, table_name, pk_name):
cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
return cursor.fetchone()[0]

But in 1.3 (at least), it reads:

def last_insert_id(self, cursor, table_name, pk_name):
# Use pg_get_serial_sequence to get the underlying sequence name
# from the table name and column name (available since PostgreSQL 8)
cursor.execute("SELECT CURRVAL(pg_get_serial_sequence('%s','%s'))" % (
self.quote_name(table_name), pk_name))
return cursor.fetchone()[0]

The difference is that 1.3 asks PostgreSQL what the name of the sequence is based on the table+pk name. The name is a little misleading as the PostgreSQL documentation indicates. It really should be like "pg_get_owned_sequence" because that's what it's asking: what is the sequence owned by this column?

It turns out if you have a pk that was created as an INTEGER and then assigned a default value of the next value of a sequence... you still don't quite have the same thing as a pk defined as serial because the column won't own the sequence.

You can rectify the situation thus:


And that will fix it. Then the pg_get_serial_sequence call will not return NULL, and your model will get an ID.

This is all something you can figure out with the documentation on the web, but I figured I'd put it here to maybe help someone, somewhere.

Wednesday, April 13, 2011

The unruly "Health" industry: some examples.

The strange medical economy we have going in the US is highlighted in one case by the story of the drug Makena, which is used to help prevent premature births. Women particularly at risk of giving birth prematurely rely on this drug.

The original drug in question, hydroxyprogesterone caproate, was made for years as Delalutin by Squibb (which became Bristol-Myers Squibb).

However, in 1999, Delalutin was discontinued by Squibb (which had been making and marketing the drug since 1959.) It was OK because "compounding pharmacies" ... which is pharmacies that can make their own compound drugs, filled the gap and sold the drug for a fairly low cost ... something like $10 to $20 per dose.

Then, a company called KV Pharmaceuticals decided it was going to market the drug by getting it FDA approved. Once they did that they'd have the sole rights to make this drug in the USA, due to patent law, as I understand it.

... Then they increased the price from about $15 a dose to $1,500 per dose. That's about $30,000 for the entire regiment that women need if they're at risk of premature birth.

Fortunately, Senator Sherodd Brown, The March of Dimes, The American Academy of Pediatrics, the American College of Obstetricians and Gynecologists, and the Society for Maternal-Fetal all protested and the FDA agreed not to restrict compounding pharmacies from making and selling the drug.

Except for these people raising their voices, the cost increase would have just happened. It was clearly the intent behind KV Pharmaceutical to charge $30,000 for a drug regimen to prevent early birth which had previously cost hundreds. (They've since cut the price in half to a mere $15,000).

Read some more details about the story here.

Health journalist Julie Applebee wrote a piece which simliarly serves as an example of the ecosystem of the health care industry, here recounted by Dr. Gregg Bloche on NPR's Fresh Air. I'll let him tell it:

Back in 2007, a so-called 64-slice CT scanner came into use, really high-resolution. Cardiologists loved it. They started buying it for their offices, and...
DAVIES: What does that mean, 64-slice?
Dr. BLOCHE: It means 64 different levels of images very close together. A CT scanner works by taking a cross section of the part of the body that it's scanning.
DAVIES: So you get a three-dimensional picture, in other words, right?
Dr. BLOCHE: Exactly. It's a three-dimensional picture that's made up from a whole bunch of slices. Imagine looking at cross sections, at multiple levels of something. You can do a cross section of the brain at multiple levels. And the closer the cross sections are to each other, the finer the resolution on the CT scan.
So 64-slice is just a fancy way of saying a really high-resolution CT scanner, so high-end resolution that you could put people in it and look at their hearts and figure out how much coronary artery blockage they had without putting a catheter inside their arteries to pump dye into their arteries, which was a rather scary way of assessing levels of coronary artery blockage.
So the cardiologists loved this. They could buy this machine and charge a huge amount for it and show beautiful pictures, stunning pictures, colorful pictures, of people's coronary arteries and the degree of blockage.
Only thing is that this test, it turn out, only turned useful for a very small number of patients who had serious coronary vascular disease. Medicare agreed to pay for it only for this small number of patients.
But the cardiologists exercised their right to petition their government. They lobbied Congress. Seventy-nine congressman from both parties wrote a letter to the agency that runs Medicare, saying: Cover this thing. Medicare soon rescinded its limiting rule and agreed to cover the test much more broadly.
So politics plays a big role in the movement of expensive technologies that yield only tiny benefits right into the marketplace. And then the developers of these technologies know that. And so they keep spending. The investment bankers know it. The venture capitalists know it.

You can read the whole interview here.

Stories like this are very important to the national health care debate and the government finance debate. The Health Reform Act passed recently doesn't seem to address any of these concerns. I'd love to see evidence that it does, but if there is, I've missed it. Nor does it touch on the ecosystem of R&D, which I haven't got time to go into now, which seems to have an equally sordid past of cannibalizing publicly funded research.

Until the ecosystem of health care is fixed and until we go back to funding research done at universities around the country, it seems like we're headed for a downward spiral of increasing costs and no one is talking about it on the national stage (probably for some of the reasons Dr. Bloche pointed out.)

Saturday, March 26, 2011

David Buss in conversation with Richard Dawkins

This is a really fun video series, David Buss has done excellent work for years but I've just recently started reading some of his work after having seen it referenced time and time again in other pop sci books about evolutionary biology/psychology.

One of the amusing things about this series is that while Dawkins had a huge influence on Buss, and Buss even uses The Selfish Gene in the classroom, Dawkins is clearly not up to date on the most recent evolutionary psychology research and theories. In a reversal of Dawkins talking with Singer, Buss is very diplomatic with his answers.

For reference, Buss' study of 37 different cultures is here.

Wednesday, January 12, 2011

Dear Recruiters: Please learn to do your job at even a minimal level of skill

This is a real email I just got. I get these kind of inquiries all the time:

Hi Dan,

I hope you are well and that you had great holiday break. I wanted to see if you are interested in a full time opportunity with one of my clients.

I am looking for a Lead Wed Developer and would like to talk to you about this open role. Best way to reach me is my cell XXX-XXX-XXXX or via email [email withheld].

Thank you again and I look forward to your reply.

[name withheld]

Oh my goodness! Yes, I will definitely jump on this amazing opportunity that you have laid out with absolutely zero details! I'm so happy to inquire about this lead since you've clearly singled me out and not sent this same letter to 50 other people who's skill sets you are not even vaguely familiar with!

I mean, I realize the economy is in a rough place. Obviously these recruiters do well enough that they haven't starved to death. (Which, for the sake of argument, we'll say is a good thing.) But for fuck's sake people, have some pride in what you do. If this was a fluke that would be one thing, but I get dozens and dozens of these emails at pretty much the same quality level.

Fortunately, I have the luxury of just deleting messages like this.