Git Product home page Git Product logo

Comments (20)

ahall avatar ahall commented on April 28, 2024 1

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.

anidotnet avatar anidotnet commented on April 28, 2024 1

@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.

jinzhu avatar jinzhu commented on April 28, 2024

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.

jinzhu avatar jinzhu commented on April 28, 2024

Ok, if anyone want to create a pull request to make it works, then I would be happen to merge it.

from gorm.

jordan-wright avatar jordan-wright commented on April 28, 2024

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.

ahall avatar ahall commented on April 28, 2024

Can we re-open this please?

from gorm.

verdverm avatar verdverm commented on April 28, 2024

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.

jinzhu avatar jinzhu commented on April 28, 2024

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.

rtfb avatar rtfb commented on April 28, 2024

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.

jinzhu avatar jinzhu commented on April 28, 2024

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.

rtfb avatar rtfb commented on April 28, 2024

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.

rtfb avatar rtfb commented on April 28, 2024

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.

jinzhu avatar jinzhu commented on April 28, 2024

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.

rtfb avatar rtfb commented on April 28, 2024

OK, so should I submit a PR with what I've done and leave the tests issue for later?

from gorm.

jinzhu avatar jinzhu commented on April 28, 2024

@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.

rtfb avatar rtfb commented on April 28, 2024

Oh, cool, I'll give it a try in the upcoming days.

from gorm.

dre1080 avatar dre1080 commented on April 28, 2024

👍

from gorm.

rvenugopal avatar rvenugopal commented on April 28, 2024

+1

from gorm.

jinzhu avatar jinzhu commented on April 28, 2024

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.

cfstras avatar cfstras commented on April 28, 2024

👍 you rock!

from gorm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.