0

domain classes:

class Carnet extends Purchasable{
    Payment payment
}

class Training extends Purchasable{
    static hasMany = [payments:Payment]
}

class Payment {
    static belongsTo = [purchase:Purchasable]
}

class Purchasable {

    Float price

    static constraints = {
    }

    static mapWith = "none"

    static mapping = {
    tablePerHierarchy false
    }
}

unfortunatelly when I try to grails run-app I've got:

ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000388: Unsuccessful: alter table payment add constraint FK_6ohgqce5txqxe8l8wkkkgjlc0 foreign key (purchase_id) references training (id)

ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Can't write; duplicate key in table '#sql-690_99'

Application is starting but carnet table in db is not created so later I receive MySQL exceptions. moreover purchasable table is created (and i do not need it at all). I tried to move Purchasable as an interface to src/groovy but I'm not sure how to do it properly. Do you know how to fix it?

Community
  • 1
  • 1
Michal_Szulc
  • 4,097
  • 6
  • 32
  • 59
  • Try deleting your database and recreate it. Alternatively you can use "create-drop" option in your application.yml – elixir Nov 13 '16 at 16:57
  • I stopped app, dropped and recreated db and recompiled project. Still the same error. – Michal_Szulc Nov 13 '16 at 17:06
  • When you mentioned that you don't need the `purchasable` table, that was a clue that inheritance is not a good fit. If you have properties that you want to define once and then share with multiple domain classes, you may be able to use Groovy traits instead (http://stackoverflow.com/questions/34918306/using-traits-for-horizontal-domain-class-reuse-in-grails-a-good-idea). However, I doubt static properties (constraints, mapWith, etc) work when implemented in a trait. So you may also want to consider using composition: http://gorm.grails.org/latest/hibernate/manual/index.html#gormComposition – Emmanuel Rosa Nov 13 '16 at 21:19
  • Another clue is to say "[sub-class] is a/an [super class]" and see if it makes sense. For example, the statement "Payment is a Purchasable" sounds odd because purchasable is more of an action rather than a noun. In contrast, "dog is an animal" makes sense; hence possibly a good fit for inheritance. – Emmanuel Rosa Nov 13 '16 at 21:24
  • At the moment I removed `static mapWith = "none"` and changed `Purchasable` name (I tried to use an interface - that's the reason of this name) to `Purchase`. But I will check out also **composition** cause it may be exactly what I am looking for. – Michal_Szulc Nov 13 '16 at 21:43
  • Try to delete your build directory and run again your application. – Rotem Nov 13 '16 at 22:14
  • 1
    I see you've defined your price as a `Float` - unrelated but you will run into issues storing currency like that https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – erichelgeson Nov 14 '16 at 21:27
  • Thank you @erichelgeson . In fact it's very important hint! – Michal_Szulc Nov 14 '16 at 22:10

0 Answers0