Commit Graph

12 Commits

Author SHA1 Message Date
jnfeinstein 8b451f0084 Add support for polymorphic relationships using the POLYMORPHIC setting.
This commit adds support for two settings:

FOREIGNTYPE - A field that is used to store the type of the owner.

POLYMORPHIC - A shortcut to set FOREIGNKEY and FOREIGNTYPE to the same
value suffixed by "Id" and "Type" respectively.

The type is stored as the table name, which I thought might be useful
for other queries.

The biggest gotcha of this commit is that I flipped the definition of
has_one and belongs_to. gorm is very flexible such that it didn't
really care if it was a has_one or belongs_to, and can pretty much
determine it at runtime. For the sake of the error, I had to define
one of them as belongs_to, and I chose the one with the fields as
the belongs_to, like ActiveRecord. The error could probably be
genericized to "gorm cannot determine type", but I think it's nicer
to tell people DONT DO PATTERN XYZ CAUSE IT WONT WORK. Functionally,
it doesn't matter.
2014-11-25 21:35:47 -08:00
jnfeinstein 794e1ba20b .Count() should always use ToSnake'd foreign keys.
It looks like gorm always uses the snake form of a column by
convention, as seen by searching DBName in scope.go. These counts
were erroring out without the ToSnake'd foreign keys.

Further, the code for has_many and has_one becomes the same (which
makes sense), so I combined the two cases.
2014-11-25 11:47:23 -08:00
Tristan Storch 9bee4239d4 FieldValueByName does now only what it should
FieldValueByName in utils.go does now only what it should and has proper errors.
2014-10-01 16:20:12 +02:00
Jinzhu 953c347ba7 Refactor Scope 2014-09-02 19:03:01 +08:00
Jinzhu 90106d82c5 Export Relationship 2014-07-31 14:38:16 +08:00
Jinzhu ba74a9545b Make clear works 2014-07-30 22:15:31 +08:00
Jinzhu 5a95050464 Make association replace works 2014-07-30 22:10:12 +08:00
Jinzhu db3da2c636 Make association Count works 2014-07-30 21:43:53 +08:00
Jinzhu 2356182ea7 Rename joinTable to relationship 2014-07-30 20:59:52 +08:00
Jinzhu e1e68debdc improve association 2014-07-30 20:48:36 +08:00
Jinzhu efd3b7678b Be able to find many to many relations 2014-07-30 16:22:26 +08:00
Jinzhu ba95de5c50 Rename relations to associations 2014-07-30 14:30:21 +08:00