Comments (6)
@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.
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.
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.
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.
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.
@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)
- Lens example in tutorial does not work HOT 1
- GHC 9.6 failures HOT 3
- beam-sqlite: No `instance HasSqlEqualityCheck Sqlite Day` HOT 1
- WHERE clauses are silently dropped if there is no FROM HOT 1
- beam-sqlite: support for `LIKE` + `ESCAPE` ? HOT 1
- free-5.2 not allowed
- vector-0.13 not allowed
- primitive-0.8 not allowed HOT 3
- How to get primary key type? HOT 1
- Subquery without aggregation
- Multicolumn aggregate HOT 1
- Is there any way to reuse a `ToField`, `FromField` definitions from sqlite-simple? HOT 1
- Precise type for `max_` aggregate function
- Postgres column doesn't roundtrip for type `Maybe (Vector Text)`
- Outdated dependencies HOT 4
- Passing `Double`s to `val_` sometimes leads to runtime errors
- run-time error "column t0.res0 does not exist" HOT 1
- Insert result of an insert
- Run-time error "window function first_value requires an OVER clause" HOT 1
- run-time error: operator does not exist: timestamp without time zone < text
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 beam.