Category: Uncategorized


Can’t create table ‘a.#sql-688_c3′ (errno: 150)

February 15th, 2012 — 9:56am

TL;DR Check the engine of both involved tables, check the character set of both involved tables, check the data type of both columns, check that the referenced column is indexed.

Yesterday I was in a fight with MySQL. I eventually think that I won by executing “sudo killall mysqld“, after which my immediate issue was fixed, but other issues emerged :) . I had been confronted with the infamous error “ERROR 1005 (HY000): Can’t create table ‘database.#sql-688_c3′ (errno: 150)”, which makes no sense and doesn’t tell me anything I actually want to know.

Of course, everyone who has run into this problem knows that you can issue the query “show innodb status\G” to see the actual issue. Now, I’ve encountered two problems with this approach, the first being that I actively have to search for an error message. The second issue though was the one that really drove me crazy; “show innodb status\G” was lying to me. Here’s what it told me:

120214 15:50:57 Error in foreign key constraint of table database/#sql-688_c3:

FOREIGN KEY ( thecode )
REFERENCES mycodes ( thecode ):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

Now, the first thing I checked was that the datatype of the column and referenced column actually matched. They did. Maybe the referenced column didn’t have an index? It did. No issues there, so I’ve asked my colleague to come over and look for any issues. He didn’t find any. If I was going crazy, so was he.

What I’ve found, is that when MySQL says ERROR 1005 (HY000): Can’t create table ‘database.#sql-688_c3′ (errno: 150)”, it actually means that one of the following things is happening:

  • The engines of the tables don’t match up (e.g. one is InnoDB, the other MyISAM). Admittedly, “show innodb status\G” will present you with another message than the one above, namely “Cannot resolve table name close to”. Don’t worry, the table does exist and you’re not going crazy, it’s just that you accidently didn’t match up the table engines.
  • The second possible reason was the one I encountered; the character sets of the tables didn’t match. The first was “Latin1″, the other was “utf8″. Yes, MySQL will tell you there’s something wrong with the data type, and you don’t see the character set when you DESCRIBE the tables. Extremely annoying.
  • MySQL might be right too. The datatype might not match, or the referenced column is not indexed. That rarely happens though, I guess.

Anyway, I hope you find this post before you start pulling out your hair.

Comment » | Uncategorized

debian: error: Cannot find php_pdo_driver.h

February 9th, 2012 — 12:50pm

I recently got so fed up with Unity, I decided it was time for me to find something new. In the process, I’ve also decided it might be best to reinstall my machine with Debian instead of Ubuntu. I’ve decided a lot, lately. Since I use PDO with PostgreSQL, I tried to install the pdo_pgsql PECL package, but it failed with the message “Configure: Error: Cannot find php_pdo_driver.h“. Now, I was pretty sure that I had that file on my box, it was located in /usr/include/php5/ext/pdo/. Nevertheless, configure couldn’t find it. Turns out it was looking in a different directory; /usr/include/php/ext/pdo.

The simplest solution I could think of:

 ln -s /usr/include/php5 /usr/include/php

It worked like a charm :)  Of course, it’d be even better to compile PHP myself, but that’s for a different day.

1 comment » | Uncategorized

Getters and setters: evil or necessary evil?

February 13th, 2011 — 5:16pm

I’ve been reading a lot about getters and setters over the last few years, including Alan Holub’s infamous article “Getters and setters are evil“, and Martin Fowler’s article “GetterEradicator.” Although I do still feel like getters and setters are to be avoided most of the time, it’s hard to tell where to use them and where you shouldn’t.

I’ve been working on the website of PFZ.nl, the largest PHP community in the Netherlands, together with about eight other programmers I can’t help but respect. While writing the code for this website, I started to notice that each and every class in the system had accessors for most, if not all, protected fields. I’ve started a discussion with my fellow developers, and this is what I’ve argued.

First, I wrote an article on it, which was rather ill-received by some. This is understandable, as the presence of accessors is deeply rooted in OO PHP code, although this isn’t limited to one specific language. Besides, my point of view sounded rather puristic in my first article. Before I want to discuss why accessors should be handled with care, I’d like to go back half a century, to the introduction of OO.

Back in the day, some great minds thought up the concept of OO to make sure software would be easier to change, which means it’s easier to maintain. To accomplish that, they thought up the concept of an Object: a small, focused unit of code that is only ever responsible of one thing. One of the key concepts of OO was that objects should hide their implementation, to let their collaborators send them messages that they could handle for themselves. This concept became known under the term “Encapsulation”, a reinforcement of the guideline “Tell, don’t ask.”

Wikipedia states that the term encapsulation is used to refer to one of two notions: either to restrict access to an objects’ internals, or a language construct that facilitates the bundling of data, and methods operating on said data (which I like to call behaviour). Since the arrival of (decent) OO functionality in PHP, I have seen a lot of accessors which don’t adhere to the above concept: they actually willingly expose implementation, instead of hiding it.

This is not necessarily a bad thing, and sometimes, this even makes sense. The problem doesn’t actually lie with exposing the implementation and the data, but with the collaborator that *directly* uses that data. The number one example of misusing exposed data would be a bank account object: you can either opt for having setBalance( $balance ) and getBalance( ) methods, but common sense would dictate different methods: withdraw, deposit, and transfer. The number one reason for these methods is that the collaborator knows *nothing* about what a balance is, nor should it have to.

Translating this example to (PHP) code, the first option with accessors would probably result into the following class and collaborator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
class BankAccount {
	protected $balance;
	public function setBalance( $balance ) {
		$this->balance = $balance;
		return $this;
	}
 
	public function getBalance( ) {
		return $this->balance;
	}
}
 
class Transaction {
	public function transfer( $amount, BankAccount $from, BackAccount $to ) {
		if( ( $from->getBalance( ) - $amount ) < 0 ) {
			throw new InsuffientFunds( );
		}
		$to->setBalance( $to->getBalance( ) + $amount );
		$from->setBalance( $from->getBalance( ) - $amount );
		return true;
	}
}

Now, although this code is easy to write, there might just as well not be a BankAccount object, as it actually doesn’t *do* anything. It’s devoid of behaviour. This means that it’s a useless object which might just as well be nuked. Refactoring to the second option (withdraw, transfer, deposit), the resulting code might be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
class BankAccount {
	protected $balance;
	public function tranfer( $amount, BankAccount $to ) {
		$this->withdraw( $amount );
		$to->deposit( $amount );
	}
 
	public function withdraw( $amount ) {
		if( ( $this->balance - $amount ) < 0 ) {
			throw new InsufficientFunds( );
		}
		$this->balance -= $amount;
		return;
	}
 
	public function deposit( $amount ) {
		$this->balance += $amount;
		return;
	}
}
 
class Transaction {
	public function transfer( $amount, BankAccount $to, BankAccount $from ) {
		return $from->transfer( $amount, $to );
	}
}

Now, this code (in my opinion) is just as easy to read and write, but there’s a very large benefits using the second approach: the ability to change. Imagine, for example, that there’s a “premium” bank account that allows to go in debt to, say, a thousand euros. You’ll have to change both examples in this case, but you’ll see that the second will accommodate this change much easier. Let’s refactor both examples, starting with the one that uses accessors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
class BankAccount {
    protected $balance;
    protected $overdraw = 0;
 
    public function __construct( $balance, $overdraw = 0 ) {
        $this->balance = $balance;
        $this->overdraw = $overdraw;
    }
 
    public function setBalance( $balance ) {
        $this->balance = $balance;
        return $this;
    }
 
    public function getBalance( ) {
        return $this->balance;
    }
 
    public function getOverdraw( ) {
        return $this->overdraw;
    }
}
 
class Transaction {
    public function transfer( $amount, BankAccount $from, BackAccount $to ) {
        if( ( $from->getBalance( ) + $from->getOverdraw( ) - $amount ) < 0 ) {
            throw new InsuffientFunds( );
        }
        $to->setBalance( $to->getBalance( ) + $amount );
        $from->setBalance( $from->getBalance( ) - $amount );
        return true;
    }
}

Although this works, it’s far from elegant. If you have more than one collaborator for the bank account object, and you usually do, you’ll have to duplicate that logic to all other collaborators. If requirements change, you’ll have to change the logic on each place you’re calculating the balance. There are more issues with the code, but I’ll get to that later. Now, to refactor the well encapsulated version of the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
class BankAccount {
    protected $balance;
    protected $overdraw = 0;
 
    public function tranfer( $amount, BankAccount $to ) {
        $this->withdraw( $amount );
        $to->deposit( $amount );
    }
 
    public function withdraw( $amount ) {
        if( ( $this->balance + $this->overdraw - $amount ) < 0 ) {
            throw new InsufficientFunds( );
        }
        $this->balance -= $amount;
        return;
    }
 
    public function deposit( $amount ) {
        $this->balance += $amount;
        return;
    }
}
 
class PremiumBankAccount extends BankAccount {
    protected $overdraw = 1000;
}
 
class Transaction {
    public function transfer( $amount, BankAccount $to, BankAccount $from ) {
        return $from->transfer( $amount, $to );
    }
}

Alright, the introduction of the new class PremiumBankAccount could have been done with the setters and getters too, but still, the change had just one line. Better yet: the change only affected the BankAccount class itself, and the rest of the application is still blissfully unaware of any change, although the functionality has been radically changed. The collaborator doesn’t know the bank account even can overdraw, nor should it have to.

If you’re still not convinced the second example is better, I’ll play devils advocate and I’ll ask you to refactor both examples to incorporate functionality so that a bank account can have a currency, say EUR and USD, and you should be able to transfer an arbitrary amount of dollars to a bank account based on EUR. You’ll see how much your Transfer class will have to do, when you’re using only accessors.

Alright, I think I’ve made my point by now: the second example is much better, and indeed, it’s OO like OO was meant to be. Now, obviously, you wouldn’t do it like I did it in the first example, right? You’d *maybe* write the accessors, but you’d put the transfer logic into the bank account itself, right? Good for you, but most classes I see out there today actually lean toward the first example, and this yields my curiosity in finding out: why?

Just open an OO library written in PHP, and grep for “function set”, and you’ll be shocked. Again, the problem doesn’t lie with the accessor per se, but you should check the collaborators, and I can guarantee you that the collaborators are doing calculations and making decisions which should belong in the object itself. By exposing the data, you’re making this possible.

If you work with others you can simply expect people to change your objects’ behaviour instead of using the accessors you wrote, or you can provide well named methods that encapsulate the data, so your coworkers know how to use your class.

So, why do we use so much setters and getters if there is seemingly no need to do so? Well, first of all, I’d like to go back a decade, when I was still in school and getting lessons on programming in Java. Indeed, my books were full of getters and setters, so I just assumed this was the way things work. How could I have known better? The people that wrote the books are notably smarter than I am, so they must be right. Right? Well, not so much.

People starting to learn OO are reading books, tutorials and example code which are littered with accessors. It just makes good sense to copy that pattern onto your own code. If, like me, you were used to writing procedural code, you’ll even feel right at home. You still manipulate the variables, only now they’re in some sort of namespaced array called an object. Awesome. I hereby bid people that write introductory tutorials to stop using accessors “because they can”, and instead only write them “because they have to”.

So, “learning it the wrong way” is a big reason, but there’s another one, and that is layering. Layering your application is supposed to make your code more maintainable, more extendable, more reusable, and more of that. In PHP, there is not a single framework that doesn’t claim to “be MVC”, and that is another reason for the presence of accessors everywhere.

If you split up responsibilities into “business logic” (such as transferring an amount to another bank account), and the logic for displaying stuff (such as, when the accounts’ balance is negative, display the balance in red), you’re supposed to be able to change one layer without changing the other. Implementing the separation between those responsibilities will make it easier to display one object in multiple ways.

For example, say I want to display the bank accounts in HTML. I’ve got a few options, one of them being rendering the HTML inside the object itself. Once you start down that road though, your BankAccount class will grow to extreme proportions, as you might want to display the information in HTML, XML, JSON, etc., etc.

Another option is to leave the displaying of the bank account to a template. This template will fetch the accounts’ balance and if < 0, display the balance in red for HTML. Indeed, this is the most widely used technique and, in and on itself, not a bad one to start with. It does have a requirement though: each part of data that you want to display will have to be available to the View layer. As PHP doesn’t know “Friend” classes like C++, and there’s no ability to read protected variables from the same “package” like Java, you’ll have to make the access public. This means that you’ll have to write a public getter for each value you want to display!

On the other side of the object, there’s storage. Generally, objects from the model layer will have to be saved somehow, and that yields the exact same issue: how does the object that saves the values (say, a DataMapper) get the values from the target object? In the exact reverse, how does a DataMapper build the object without being able to set values? You could use the constructor for this, but that might mean you get a constructor with 10+ parameters, which is not the most clean solution ever.

All in all, I don’t think accessors are evil per se, but you should only ever use the accessors in cross-layer situations. Don’t use accessors to build functionality, but only because you need the value to display and/or save.

22 comments » | Object Oriented, programming, Theory, Uncategorized

Back to top