Coder Social home page Coder Social logo

Foreign key column name incorrect about beam HOT 6 CLOSED

karls avatar karls commented on August 16, 2024
Foreign key column name incorrect

from beam.

Comments (6)

tathougies avatar tathougies commented on August 16, 2024

@karls
I believe this is expected behavior. If you want to rename the foreign key column name, you can use the method described here to rename the primary key column.

To be clear, the default naming rules for foreign key columns is to take the name of the foreign key record name (customer_id in your case) and appending a __ and the default name of the column in the related table (id in this case).

If you have any other questions (including suggestions on better defaults), feel free to comment on the mailing list

from beam.

karls avatar karls commented on August 16, 2024

Hey, thanks for the clarification and the suggestion! I tried to modify the foreign key field name, but it does not seem to be possible? According to https://github.com/tathougies/beam/blob/master/beam-core/Database/Beam/Schema/Tables.hs#L132 field modifications only work on Columnar-s?

from beam.

tathougies avatar tathougies commented on August 16, 2024

Indeed, you can only rename a Columnar, but an embedded primary key is composed of columnars.

In the Chinook schema, we rename all the columns, including foreign keys embedded in other tables. We use the IsString instance of FieldModification.

So basically (making it very explicit) if you have

data CustomerT f
  = Customer
  { customerId        :: Columnar f Int32
  , customerFirstName :: Columnar f Text
  , customerLastName  :: Columnar f Text
...
  } deriving Generic
instance Beamable CustomerT
type Customer = CustomerT Identity; deriving instance Show Customer

instance Table CustomerT where
  data PrimaryKey CustomerT f = CustomerId (Columnar f Int32)


data InvoiceT f
  = Invoice
  { invoiceId       :: Columnar f Int32
  , invoiceCustomer :: PrimaryKey CustomerT f
...
  } deriving Generic

Then you can do

  defaultDbSettings `withDbModification`
  (dbModification
   { ...
   , invoice = modifyTable (\_ -> "Invoice")
                   (tableModification { invoiceCustomer = CustomerId (fieldNamed "CustomerId") })
    })

from beam.

karls avatar karls commented on August 16, 2024

Oh! Thanks for spelling this out for me — I got my example working as well now.

There is a fair number of types jiggle around and learn how they all fit together, but I'm hopeful. I will continue to evaluate beam for our project. :-)

Btw, appending pattern of appending __id — I hadn't come across it before.

from beam.

tathougies avatar tathougies commented on August 16, 2024

Awesome! Feel free to ask any more questions on the discussion list.

Btw, appending pattern of appending __id — I hadn't come across it before.

Just an easy way of generating unique column names 😄

from beam.

wraithm avatar wraithm commented on August 16, 2024

@tathougies This should definitely be documented better. It took me a second to figure out what's going on with foreign key field names.

from beam.

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.