Will Dapper.net work if I am using a property other than "Id" or "id" for the primary key of a table / object?
All I can see in the docs is something about "split on" which I do not follow.
Will it work for mapping inserts, updates & selects without being aware what the primary key is / without having an Id property?
Is anything else going to be different or not work by not having an Id field?
Will Dapper.net work if I am using a property other than "Id" or "id" for the primary key of a table / object?
Anytime without an issue, dapper will have no issue with this, dapper, being an object mapper, doesn't care much about the primary key
Except That
When you do Complex object mapping, which contains custom types, then with Id it can automatically split in the Query call and fill the objects. This is since they need some standard to fill objects, which can be overridden by supplying the SplitOn
Related
There are a lot of questions asking this but I can't seem to find one with the specific solution.
I have a 3rd party database where every field is set to allow null. One of the columns ("Code") is a unique string ID and it is distinct.
Using entity framework I'd like to add this table, by telling EF to treat the column "Code" as a primary key.
I've created a view but I am not sure where to go from here.
I've seen some solutions that involve adding an extra row number to use as the primary key but I would prefer to use "Code" if possible.
Any ideas?
After some playing around I found a read-only solution
In the view I modify the column to be:
SELECT ISNULL(Code, -1) AS Code
Specifying ISNULL allows EF to infer a primary key. It is not ideal as I would like it to be writable as well. You get the message:
Error 6002: The table/view 'KittyCat.dbo.View_GetCatDetails' does not
have a primary key defined. The key has been inferred and the
definition was created as a read-only table/view.
We are using Hazelcast for in memory data grid. We want to extend it for analytic using in memory computation.I have few question regarding this
Which data structure to use ? (I do not have primary key as de-normalize table and have a huge data )
If IMap the only option then can we use composite key or dummy key which should have support for index and predicate?
This is not the right use case i.e Hazelcast can not used for analytics?
You can generate random keys based on UUID::randomUUID or you can create composite keys. Indexes can be created over values and keys (for keys use the magic keyword __key# and add the property of the key you're interested in.
Predicates use the same keyword if you're looking to run it against a composite key property, otherwise just query as you expect it from any other data.
I need to store an entity type and I would like to query it once by its key (e.g. it's userID) and once by its parentKey (e.g. the adminUserID of userID). Is it a such thing possible?
I was thinking to create a key using the userID as key and then add the adminUserID as parent/ancestor key. Am I still able to get the entity by its "simple" key (userID) or I always need to create the key both from the ancestor key and the userID ?
Once a key is created, it cannot be changed, i.e. you cannot add a parent to an existing entity (you'll have to delete it and create a new entity with a parent key). You can create a key with a non-existent parent, though, as long as you know its id beforehand.
Also note that children entities may have the same auto-generated ids if they belong to different parents.
I don't know your requirements, but you may be better off with a simple no-parent entity where parent id is just an indexed property. You can create such entities at any time, add parent ids at any time, and query both by id and/or parentId.
In ORMLite, how can I define a constraint (foreign) to another table, which is not linked by it's integer id, but by any other field, i.e. a varchar/string field, which isn't the PK.
For example, referring to the ORMLite sample code, where an 'order' entity is linked to an 'account' entity. In the example, the order is linked to the account by it's id column (I guess by default), which is the PK.
Instead of setting up the constraint from 'order.account_id' to the 'account.id' column, how can I set it up from something like 'order.account_name' to 'account.name' column instead?
I was looking for something like a foreignColumnName annotation, but unfortunately it doesn't exist, for a one-to-one relationship.
Here's the java code of the ORMLite examples:
Order.java
Account.java
I couldn't find any info in the documentation.
(The reason why I need it is that I have an existing db, not created by ORMLite automatically, which has obviously not been setup properly nor halfway normalized, but I need to work with that existing one, including it's existing column names and constraints.)
I was looking for something like a foreignColumnName annotation, but unfortunately it doesn't exist, for a one-to-one relationship.
Edit:
This feature was added to ORMLite in version 4.36. The concept of foreign objects was a new construct at the time. See the javadocs on the field here. To quote:
public abstract String foreignColumnName
Name of the foreign object's field that is tied to this table. This does not need to be specified if you are using the ID of the foreign object which is recommended. For example, if you have an Order object with a foreign Account then you may want to key off of the Account name instead of the Account ID.
NOTE: Setting this implies foreignAutoRefresh() is also set to true because there is no way to refresh the object since the id field is not stored in the database. So when this is set, the field will be automatically refreshed in another database query.
I want to make a primary key from 2 fields in Django.
Fields are below
- current Time
- userId
How Can I do this??
Time + userid {PK } [ Current Time + userid]
Thank you!!
Most of the time, you don't actually need your multi-column key to be the primary key.
Django operates best with surrogate keys - that is, it automatically defines an autoincrement field called id and sets that to be the primary key. That suits for almost all uses.
If you then need to enforce a unique constraint on your model across two or more fields, you can use the unique_together setting in the inner Meta class.
It's not built into Django (see #373), but see the bottom of http://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys for the discussion. I've copied over the "alternative methods" section here for posterity.
Alternative methods
notnotpeter: Currently, you can "fake"
it by declaring one of the keys to be
primary in Django and adding a unique
constraint to the model. (needs more
info...examples?)
mjm: This only works when there is a
non-compound unique key to use, if I
understand what's being proposed here.
As such, it may be workable as a way
to squeeze a design that naturally has
CKs into Django, but it's of no use
for working with an existing schema
that has only the CK.
djansoft: It can be done using
unique-together.
Tobu: You can't use just one key. A
use case for illustration: a
NamespacedTag? model with a CK made of
a namespace and a name. The primary
key can't be just the namespace or
just the name since that would be
ambiguous. The only solution (and I
dislike it since it is bad modelling
and forces database accesses to be
serialized on a counter) is to use an
AutoField?.
toszter: Call me nutty, but why not
declare any number of columns as
primary key in your model, which
django then uses to create a "hidden"
pk_composite column that's just a hash
of the three pk values in the row?
When you do a lookup on a pk, you
assume the ENTIRE combination of
values is the primary key, nothing
more or less. So when you look up
according to the values needed to
create the full pk_composite, the hash
always computes and if the hash
algorithm is public, can be used off
the URL, the querystring in db
lookups, and just about anywhere.
Seems pretty simple in my mind but
then again I am not going into great
detail here.
That being said, I would create an ID on the table and make that the primary key. Then add a unique contraint on the user_id + timestamp.
If the key has to be single-column:
id is added "out-of-the-box" (variable.id) and for current time just add
from django.db import models
date = models.DateTimeField( auto_now_add=True)
in yourapp/models.py
so you have your 2 values, out of witch you can make your key in any shape you want.
if you want multi-column-key then read the other answer. Hope to help you.