2015-04-19 2 views
0

Я работал над приложением Grails. Я создал 3 объекта Customer, Product и Order. Клиент может иметь много заказов, но каждый заказ может содержать только один продукт. Таким образом, один заказ может быть связан с 1 клиентом и 1 продуктом. Вот мои домены:Доменная ассоциация между 3 объектами

Заказчик:

class Customer { 
    String name; 
    String address; 
    Integer phoneNumber; 
    Date dateCreated 

    static hasMany = [orders:Order] 
    static constraints = { 
     name nullable: false 
     address nullable: false 
    } 
} 

Заказ:

class Order { 
    Customer customer 
    Product product 
    Date orderDate 

    static hasOne = [customer:Customer,product:Product] 

    static constraints = { 

    } 
} 

продукта:

class Product { 
    String name; 
    String description; 
    Date dateCreated 

    static hasMany = [orders:Order] 

    static constraints = { 
    } 
} 

Я создал контроллер, вид с помощью генерации-все, и когда я бегу приложение я получаю эту ошибку:

| Running Grails application 
| Error 2015-04-19 17:22:35,289 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: create table order (id bigint not null auto_increment, version bigint not null, customer_id bigint not null, order_date datetime not null, product_id bigint not null, primary key (id)) ENGINE=InnoDB 
| Error 2015-04-19 17:22:35,290 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order (id bigint not null auto_increment, version bigint not null, customer_id b' at line 1 
| Error 2015-04-19 17:22:35,373 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: alter table order add index FK651874E2B2D0780 (product_id), add constraint FK651874E2B2D0780 foreign key (product_id) references product (id) 
| Error 2015-04-19 17:22:35,373 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order add index FK651874E2B2D0780 (product_id), add constraint FK651874E2B2D0780' at line 1 
| Error 2015-04-19 17:22:35,373 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - Unsuccessful: alter table order add index FK651874E89AD9194 (customer_id), add constraint FK651874E89AD9194 foreign key (customer_id) references customer (id) 
| Error 2015-04-19 17:22:35,373 [localhost-startStop-1] ERROR hbm2ddl.SchemaUpdate - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order add index FK651874E89AD9194 (customer_id), add constraint FK651874E89AD919' at line 1 

Также я не могу создать клиента. Что-то не так в области отображения домена?

Мой источник данных конфигурации выглядит следующим образом:

dataSource { 
    driverClassName="com.mysql.jdbc.Driver" 
    dialect="org.hibernate.dialect.MySQL5InnoDBDialect" 
    url="jdbc:mysql://localhost:3306/aprilapp?useUnicode=true&characterEncoding=UTF-8" 
    username="root" 
    password="root" 
    dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', '' 
} 
+0

Возможный дубликат [Создание таблицы не удается] (http://stackoverflow.com/questions/10195397/table-creation-fails) и [Grails: can not create tables on mysql] (http://stackoverflow.com/questions/23016719/grails-cant-create-tables-on-mysql) – user1690588

+0

@ user1690588 Я думаю, что использую правильный диалект. –

ответ

1

Вы назвали вашу таблицу order, и это зарезервированное SQL ключевое слово. Выберите другое имя таблицы.

+0

Ах .. Я этого не заметил. Благодаря! –

Смежные вопросы