Having Problems Creating Simple Relationship

[prev] [thread] [next] [Date index for 2004/08/14]

From: brian
Subject: Having Problems Creating Simple Relationship
Date: 01:07 on 14 Aug 2004
I'm following the documentation on CPAN and I'm having problems getting a simple one-to-many relationship figured out.  Basically, every time I insert into the parent table, then insert into the child table, it's giving me the error:

Can't insert new Store::LineItem: DBD::mysql::st execute failed: Column 'order_id' cannot be null [for statement ``INSERT INTO sf_order_line_items (color, product_id, order_id, price, qty, size)
VALUES (?, ?, ?, ?, ?, ?)
''])

None of the documentation (that I have found) specifies (when inserting a child)how to tell the class what value to use for the foriegn key.  So I'm not passing an order_id, even though it's required (shouldn't it deduce that from the fact that I'm calling it from the parent class?).  No matter what I specify for the order_id, I still get the same error.  I've tried 'NULL', undef, any integer, anything, I always get that error.

I'm assuming I set up the relationship wrong somehow, so here is my code in hopes that you can give a bit of help.

<code>
#==============================================================================
package Store::LineItem;
use base 'Store::DBI';

Store::LineItem->table('sf_order_line_items');

Store::LineItem->columns(qw(
	id order_id product_id qty price color size custom_opts
));

Store::LineItem->has_a(order_id => 'Store::Order');

#==============================================================================
package Store::Order;
use base 'Store::DBI';

# This is here to emulate the default placed_on = NOW()
__PACKAGE__->add_trigger(before_create => sub { 
	my $class = shift;
	return unless $class->find_column('placed_on');
	$class->placed_on($class->get_current_time());
});

Store::Order->table('sf_orders');

Store::Order->columns(qw(
	id transaction_id auth_msg successful card_number exp_date amount
	billto_fname billto_lname billto_address billto_city billto_state
	billto_zip billto_country billto_phone shipto_fname shipto_lname
	shipto_address shipto_city shipto_state shipto_zip shipto_country
	shipto_phone placed_on shipped_on backordered_on canceled_on company_id
	customer_id cart_id shipping_method shipping_cost tax tracking_no
	shipped_by deleted gift gift_message affiliate_id affiliate_paid
	shipto_company giftcert_id credit_used printed promo_id promo_amount cv2
	authorizedotnet_code authorizedotnet_captured authorizedotnet_txid
	remote_addr
));

Store::Order->has_many(line_items => 'Store::LineItem');
</code>

And then in the main perl script I have:
<code>
use TestOrder;

my $o = Store::Order->create({
	cart_id => '898996',
	transaction_id => '823498723498',
	successful => 1,
	card_number => '1234123412341234',
	exp_date => 1104,
	amount => 50.99,
	billto_fname => 'Brian',
	billto_lname => 'Lozier',
	billto_address => '123 Happy Street',
	billto_city => 'Belfair',
	billto_state => 'WA',
	billto_zip => '98528',
	billto_country => 'USA',
	billto_phone => '1231231234',
	company_id => 1198,
	customer_id => 125998,
	shipping_method => 'Standard',
	shipping_cost => 5.00,
	tax => 0.00,
	deleted => 0,
	gift => 0,
	cv2 => 123,
	authorizedotnet_captured => 0
});

my $l = $o->add_to_line_items({
	order_id => 5, # No matter what I set this to, I get the error - even if I remove it completely!
	product_id => 73856,
	qty => 1,
	price => 45.99,
	color => 'green',
	size => 'ML'
});
</code>
Any help would be greatly appreciated.

Having Problems Creating Simple Relationship
brian 01:07 on 14 Aug 2004

Re: Having Problems Creating Simple Relationship
Brian E. Lozier 01:24 on 14 Aug 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52