Can't get line_items table created

I try to execute this script on Mysql and it can’t create it and get
this error in MySQL query browser window.
“Can’t create table ‘.\pos_development\line_items.frm’ (errno: 150)”

The problem is with the “constraint fk_items_product” part of the
script.

Help!!!

create table line_items (
id int not null auto_increment,
product_id int not null,
quantity int not null default 0,
unit_price decimal(10,2) not null,
constraint fk_items_product foreign key (product_id) references
products(id),
primary key (id)
);

Try changing the order in which you’re creating the tables.

hth,
Bill

[email protected] wrote:

I try to execute this script on Mysql and it can’t create it and get
this error in MySQL query browser window.
“Can’t create table ‘.\pos_development\line_items.frm’ (errno: 150)”

The problem is with the “constraint fk_items_product” part of the
script.

Checking the documentation here:
http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
… it looks like “constraint” isn’t valid syntax within the table
definition (and constraints don’t actually get names like
fk_items_product). Your table definition should look like:

CREATE TABLE line_items (
id INT NOT NULL AUTO_INCREMENT,
product_id INT NOT NULL,
quantity INT NOT NULL DEFAULT 0,
unit_price DECIMAL(10,2) NOT NULL,
FOREIGN KEY (product_id) REFERENCES products(id),
PRIMARY KEY (id)
);

I haven’t tried this myself (I’m using PostgreSQL), but it looks right
according to the docs. (If it doesn’t work, hopefully that page should
help you out further.)

Having said that, you really should look into using migrations to define
your database rather than creating it directly with raw SQL. They’re
more flexible, easier to work with, database-independent, and are
currently considered the correct, “modern” way to define a Rails
database schema.