Comments (20)
Ouch, so it's impossible to use GORM with legacy db schema. That's ridiculous. Should be able to specify your own column and table names and if not specified default to the convention. Should be able to do this:
Blah string sql:"name:bleh"
Probably something similar for the table name. Hope you reconsider this as your project looks really promising. If not I'll have to find something else.
from gorm.
@jinzhu sorry to write comment on a closed issue. But I also think @ahall is right. Atleast this awesome library needs to have flexibility like this. I am also facing similar issue, where I need to specify column name explicitly. Requesting earnestly to have a second thought on this and if possible, please give us an awesome solution. Thanks for your great effort... 👍
from gorm.
Hi @yulrizka
Sorry for the delay, I have give it some thoughts.
I don't really like the idea because it would slow down all queries/updates due to have to check the tag every time.
Also it would be quite confusion if you have two running applications with different names for same column. So I decided to don't implement it.
Anyway, Thank you for your suggestion.
from gorm.
Ok, if anyone want to create a pull request to make it works, then I would be happen to merge it.
from gorm.
I love the gorm package, and am appreciative of all your hard work.
I don't have a pull request, but I, too, would really benefit from being able to specify the column name. For example, acronyms are capitalized in Go. As such, I have a struct field APIKey
. I would like very much to use the column api_key
, but with Gorm's current naming strategy, the table is a_p_i_key
, which is hardly readable.
from gorm.
Can we re-open this please?
from gorm.
I also noticed that I have to manually convert to the same name that Gorm translates field names to when using many functions
this would be a useful enhancement
from gorm.
Ok, thanks for all your guys's suggestions, I will reopen this issue now and think if there are any nice solution to could resolve those issues.
Any suggestions are welcome!
from gorm.
I have been fiddling with this issue for some time now and figured I should ask for feedback.
Here's the simplest possible solution to the problem: https://gist.github.com/rtfb/bc60c54f0c84792c5db4
Note the usage, it's name:"field_name"
, not sql:"name:field_name"
as was suggeseted by @ahall. Both ways seem logical to me, I couldn't decide on my own, so what do others think? The latter form is significantly more difficult to do, but I have a WIP patch for it too.
Another question is how can we test this functionality? Calling db.CreateTable(MappedFields{})
is easy, but how can I test that it really did the proper job in a db-agnostic way? I was thinking of grabbing the raw CREATE TABLE
statement and looking for clumn name substrings in it, but that seems kinda crude.
from gorm.
I prefer the tag named like sql:"column:field_name"
, but column:field_name
is also acceptable for me (I think this would be easier to implement, and performance would be better?).
Another question is how can we test this functionality? Calling db.CreateTable(MappedFields{}) is easy, but how can I test that it really did the proper job in a db-agnostic way?
You could check column exist or not by using db.NewScope(&MappedFields{}).HasColumn("field_name")
after migration.
from gorm.
OK, so I sat down last night to shape up the details of this and ended up with this: rtfb@837113b
The problems is, I have hit mysterious test failures after this change. After this commit a couple of tests started failing on me consistently:
--- FAIL: TestUpdate (0.11 seconds)
main_test.go:1095: Product 123 should be changed to 789
main_test.go:1103: Product 456 should be changed to 789
main_test.go:1123: Animal 'Fredinand' should be changed to 'Franz'
main_test.go:1131: Product 'nerdz' should be changed to 'Franz'
--- FAIL: TestUpdates (0.04 seconds)
main_test.go:1187: Product abc's code should be changed to fgh
main_test.go:1201: We should have Product fgh
Looking at the log of SQL statements, here's the biggest head scratcher:
UPDATE animals SET "mapped_name" = 'Franz', "mapped_time" = '0001-01-01 00:00:00 +0000 UTC' WHERE name in ('Ferdinand')
Needles to say, I'm thoroughly surprised by these column names being used on a wrong table. @jinzhu, do you have any ideas how could this happen? Did I uncover a subtle bug elsewhere or did I sneak one in?
(If somebody is going to check this out, here's the branch: https://github.com/rtfb/gorm/tree/mystery)
from gorm.
A little update. It seems that tests are not insulated enough from each other. I can work around the above problem by simply moving TestMappedFields
to the end of main_test.go
. There really seem to be some nasty problems deep inside gorm (or the way tests are organized).
from gorm.
Yes, I agree with you about the tests are not well organized... but it would take some time to refact the 2000 lines+ test file, so still haven't start it...
from gorm.
OK, so should I submit a PR with what I've done and leave the tests issue for later?
from gorm.
@rtfb I have already refactored the tests, should be much better now, so you only need to merge the latest code into your branch.
from gorm.
Oh, cool, I'll give it a try in the upcoming days.
from gorm.
👍
from gorm.
+1
from gorm.
Hi @ALL
Finally I have finished this feature request, please refer the README for the usage.
https://github.com/jinzhu/gorm#existing-schema
from gorm.
👍 you rock!
from gorm.
Related Issues (20)
- PostgreSQL inet data type support
- Use *DB.Row(sql string, values ...interface{}) method directly occur Error 1040: Too many connections HOT 1
- Use *DB.Raw(sql string, values ...interface{}) method directly occur Error 1040: Too many connections HOT 2
- Simplify relation mapping between tables
- unsupported relations for schema ExamPaper
- Is gorm supports multi-rows update? HOT 1
- Why `Open` support multi `Option` but only apply the last one after sort?
- [BUG] `strings.(*Builder).WriteByte` memory leak in `.FirstOrCreate` with `PrepareStmts: true` setting HOT 2
- Is the issue of zero values in gorm struct solved?
- how can i read data that "with totals" from clickhouse? HOT 1
- Upgrade 1.25.5->1.25.7: AutoMigrate returns the error "insufficient arguments" HOT 6
- bug: first argument in the params is not binary
- 1.2.7 breaks migrations when updating 1.2.5 HOT 10
- clause/expression.go does not provide any mechanism to escape ? literal inside SQL statement
- Mysql在一对多情况下会双倍插入修改的外键内容 HOT 2
- Belongs To Relation failed when owner's field name is same to foreignKey HOT 1
- Default value for text type is not showing in generated structs HOT 1
- Support duckdb HOT 1
- How to get transaction ID by gorm? HOT 1
- SQL printed to terminal is error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gorm.