Commit Graph

1273 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 1de020dc4d Add test for polymorphic associations. 2014-11-25 21:27:08 -08:00
jnfeinstein 7e8c0f7edd Add README.md updates for polymorphism. 2014-11-25 21:27:08 -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
Jinzhu a4e0ef6509 Refactor reuse existing database connection 2014-11-25 15:21:28 +08:00
jnfeinstein 1d6b59f223 Allow .Open to reuse existing database connections.
This commit allows you to pass a string or an existing database
connection as the source for gorm. The dialect is still required
because a) there is no common reference to it as far as i know, and
b) gorm allows the dialect to differ from the driver. So, for the sake
of simplicity, you still have to specity the dialect.

This is useful if you have an existing transaction, but still
want to use gorm to format your queries.

This is dependent on the defintion of DB in pkg database/sql having
the field 'dsn', which is the database source, obtained via reflect.
2014-11-25 15:21:28 +08:00
Jinzhu 0e22837c04 Merge pull request #297 from westonplatter/readme-add-wrecker
readme. add wrecker master branch badge to readme.
2014-11-25 15:15:08 +08:00
Jinzhu a7e63541fd Fix migration tests 2014-11-25 14:41:09 +08:00
Jinzhu e9684db42a Show error message if not using addressable value for auto migration 2014-11-25 13:44:14 +08:00
Jinzhu 1aaac379ae Fix tests for mysql, postgres 2014-11-24 18:16:07 +08:00
Weston Platter e56f82e256 readme. add wrecker master branch badge to readme. 2014-11-23 21:38:57 -07:00
Jinzhu 84954e6779 Merge pull request #284 from shirou/master
fix panic problem when the struct has unexported field.
2014-11-21 10:29:55 +08:00
Shirou WAKAYAMA e313827f04 fix panic problem when the struct has unexported field. 2014-11-19 12:44:57 +09:00
Jinzhu f43456fecf Merge pull request #282 from jnfeinstein/master
Add additional methods of specifying the 'select' portion of a query.
2014-11-18 20:34:24 +08:00
jnfeinstein 429a100856 Add additional methods of specifying the 'select' portion of a query.
This commit adds more ways of specifying selects:

-) You can now pass in a []string.  This is mostly for convenience,
since you may want to dynamically create a list of fields to be
selected.

-) You can now use variables.  This is important because a select
could take user input.  For example, finding a MAX between a record
and a given number could be easily done using select, and then
you don't have to process anything in backend logic.  This is also
necessary to use postgres text search capabilities (which actaully
play nicely with the rest of gorm).

-) You can now chain select calls.  This could be useful in
conjunction with gorm's scopes functionality.
2014-11-17 07:36:26 -05:00
ryanss 62b447bc9a Create tables with fields in same order as defined in struct 2014-11-17 07:36:26 -05:00
Jinzhu 8482d0a121 Merge pull request #280 from ryanss/master
Create tables with fields in same order as defined in struct
2014-11-17 17:50:09 +08:00
Jinzhu 75819b2bb3 Refactor DefaultValue 2014-11-17 17:38:32 +08:00
ryanss d3097687f7 Create tables with fields in same order as defined in struct 2014-11-16 00:42:36 -05:00
Paolo Galeone 064d91335b Add default values support ( https://github.com/jinzhu/gorm/issues/251 ) 2014-11-15 18:32:35 +01:00
Jinzhu a405f08f3a Merge branch 'BugHert push origin master-fix-one-to-one-relation' 2014-11-12 10:25:43 +08:00
Jinzhu b1c2a22646 Remove unnecessary ToSnake 2014-11-12 10:23:54 +08:00
Jinzhu b32c5badd2 Refactor getInterfaceAsSql 2014-11-12 10:09:06 +08:00
Jinzhu 828bf5d5fd Fix can't update record with customized primary key 2014-11-12 10:09:06 +08:00
Paolo Galeone da3e74ba6f Add support for signed and unsigned integer types as primary key type 2014-11-12 10:09:06 +08:00
Jinzhu b261296b76 Merge branch 'galeone-master' 2014-11-11 13:36:48 +08:00
Jinzhu dcdbb72570 Refactor getInterfaceAsSql 2014-11-11 13:36:12 +08:00
Jinzhu aab0b28457 Fix can't update record with customized primary key 2014-11-11 12:15:07 +08:00
Jinzhu 010e7a90b3 Fix can't update record with customized primary key 2014-11-11 11:46:21 +08:00
deoxxa c041e53612 fix query generation for one-to-one relations 2014-11-06 10:52:23 +11:00
Paolo Galeone 4c8e6a095a Merge remote-tracking branch 'upstream/master' 2014-10-29 13:46:41 +01:00
Jinzhu a29ac54e48 Limit condition should not be inherited by following queries 2014-10-28 17:18:11 +08:00
Jinzhu 48930b60eb Fix spelling error in README 2014-10-28 17:01:05 +08:00
Paolo Galeone c108cf022b Add support for signed and unsigned integer types as primary key type 2014-10-22 17:33:13 +02:00
Jinzhu 52ba1858c3 Merge pull request #247 from arianitu/master
Update README.md
2014-10-15 21:27:48 +08:00
Jinzhu 6c1ca7adb8 Include ignored columns in Fields(), it could be used as cache field 2014-10-14 00:06:41 +08:00
arianitu 716e682394 Update README.md
Make the connection string for MySQL more obvious.
2014-10-11 21:40:10 -04:00
Jinzhu 5d0d2a8e98 Merge pull request #244 from rtfb/fix-ignored-field-bug
Fix a bug with custom columns and ignored fields
2014-10-10 08:34:32 +08:00
Vytautas Šaltenis 67f71b2f86 Fix a bug with custom columns and ignored fields
An ignored field should also be ignored when column names are being
determined.
2014-10-10 00:05:15 +03:00
Jinzhu 0aaefebf4f Add support for custom column names 2014-10-07 22:37:33 +08:00
Jinzhu 5b5ee62d06 Merge pull request #241 from tstorch/if_to_switch
Cheanged if to switch in scope.go
2014-10-07 21:56:04 +08:00
Tristan Storch 41eeeeea7c Cheanged if to switch in scope.go
Readability improved by changing a long if-else to a switch statement.
2014-10-06 11:11:07 +02:00
Jinzhu 375f3ee260 Merge pull request #239 from tstorch/cleanup
FieldValueByName does now only what it should
2014-10-03 13:32:18 +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
Tristan Storch f1212ecd58 Readability update
Small readability update. Includes also a switch from bool return values to
error return values in Scope.SetColumn and Field.Set.
2014-09-30 14:02:51 +02:00
Jinzhu 591d4a4057 Merge pull request #235 from ehabit/patch-1
Fixed typo on line 196, changed works to work.
2014-09-28 07:27:42 +08:00
ehabit bdadbb5c6f Fixed typo on line 196, changed works to work.
Fixed typo on line 196, changed works to work.
2014-09-27 15:35:18 -07:00
Jinzhu eb7ba964b2 Merge pull request #234 from tstorch/small_refactor
Minor Refactoring for readability
2014-09-26 21:56:39 +08:00
Tristan Storch f771ee21da Minor Refactoring for readability
Refactored of a small portion if the scope code for readability
2014-09-26 15:35:04 +02:00
Jinzhu 81874feef9 Merge branch 'thecaddy-master' 2014-09-19 21:51:06 +08:00