Comments (9)
Can you give exact exception stack trace instead of just the error message, please? Thanks.
On Sep 16, 2011, at 3:54 PM, Aleksey Malevaniy wrote:
When there is:
@entity public class Message extends Model{ @PrimaryKey(autoIncrement = true) public int rowId; @Index public int id; public int title; public int text; @OneToOne public Message parentMessage; }error occurs saying
column parent_message is not unique
It's a some kind of restriction, but! One should feel free to use unique indexes even in this situation.
P.S. everything works if I use set @Index as not unique.Reply to this email directly or view it on GitHub:
#33
from orman.
Surely, sorry. Here it is:
Caused by: org.orman.dbms.exception.QueryExecutionException: Query execution error: SQLiteAndroid error:android.database.sqlite.SQLiteConstraintException: column parent_message is not unique: INSERT INTO ...
from orman.
Now you can see that it is not an Orman constraint but Android SQLite implementation constraint. We also were not aware of that. If you can share your app logic with us (which column is referenced by parent_message
e.g.) maybe we can help.
But in most cases, parent side of foreign keys has to be unique fields since they directly point a single entry and duplicate values on that field may cause confusion. This is not related to the software that you are creating, it is about relational database theory.
from orman.
Oh, now I see, it's really obviously, thanks. Sorry for bothering.
BTW, some words about my app logic and orman experience: as I'm writing some kind of messenger, I have to use wrapper methods for insert/update objects to keep consistency. So I have some ideas how framework could help with this work even in complicated situation. I'll try to share app logic and code this weekends ;-)
from orman.
One more thing.
In my app I definitely have to live without UNIQUE index when creating @OnetoOne relation. In example above you can see that Message has a reference to parent Message (it creates cascade, 'cause one message could be a comment to another). And some messages has null as a parent (here comes unique violation).
Is there is a way to create NOT unique index for @OnetoOne? Or how you recommend to deal with this situation?
from orman.
You can just store an integer in that field and just say that private int parent_message
in your entity class, without any annotations.
Then you can use ModelQuery
to query object with that id. I think we haven't undocumented ModelQuery
yet.
I will think that you have an @entity called Message
and it has an int
field id
. It should be:
Message parentMessage = Model.fetchSingle(
ModelQuery.select().from(Message.class)
.where(C.eq(Message.class, "id", 99)).getQuery(),
Message.class);
Here you should note that
- "id" is field name as appears in @entity Java class
- You should replace value of
99
withyourInstance.getParentMessage()
(which returns id that you store)
This query returns null
if not found. With that solution what you do is to actually storing just integer value of the parent message id
in a field and then querying it manually. If you wouldn't have that constraint, you would be using @OnetoOne and your getParentMessage()
will be returning Message
instance whose id
is stored in that field automatically.
from orman.
Thank again!
I understand this approach but I tried to avoid it, because everybody wants to work with objects without any hacks :) I also tried to set unique=false but as you wrote earlier it causes sql db to break down...
Now I think it would be interesting to find out how other ORMs deal with it. Maybe, there is a solution... I'll try to investigate.
from orman.
I think none of the relational DBMSes on the earth would allow you to create FK to non-unique index
ed fields. Therefore none of the ORMs will be handling it specially. Assume that I say that my parent is 99. But in the database id:99 corresponds to two different values. Who is my parent now?
If you are going to have multiple parents, then you should use @OneToMany
on parent
field, that verbally means I (one) have (many) parents.
from orman.
Your're right and my weak knowledge of RDBMS.
The approach with storing only int value and fetching object by it
works well enough.
Regards,
Aleksey Malevaniy
On 16 September 2011 17:23, ahmet alp balkan
[email protected]
wrote:
I think none of the relational DBMSes on the earth would allow you to create FK to non-
unique index
ed fields. Therefore none of the ORMs will be handling it specially. Assume that I say that my parent is 99. But in the database id:99 corresponds to two different values. Who is my parent now?If you are going to have multiple parents, then you should use
@OneToMany
onparent
field, that verbally means I (one) have (many) parents.Reply to this email directly or view it on GitHub:
#33 (comment)
from orman.
Related Issues (20)
- Model.update() problem HOT 6
- SQLite Missing Boolean Type Causes Invalid SQL
- Issue creating one-to-many relationship with Id autoincrement HOT 2
- Class extending Model and another Class HOT 1
- Error query Inserting % ' HOT 9
- Android - MappingSessionAlreadyStartedException HOT 12
- org.orman.mapper.exception.FieldNotFoundException: Could not find property (field) HOT 5
- java.lang.NullPointerException HOT 1
- android.database.sqlite.SQLiteException: Can't downgrade database from version 35 to 33 HOT 6
- Enum Fields HOT 2
- sql aggregate functions
- QueryBuilder Max and Min Problem
- Cannot set Nullable UNIQUE constraint on a column (SQLite on Android) HOT 3
- Sqlite Double Mapping Problem
- mysql can't set characterEncoding when connection?
- Model entity update bug HOT 8
- JoinType has problem when selecting other options and only works when JoinType.JOIN is selected. HOT 1
- how to map time to mysql HOT 1
- How to connect two db at the same time HOT 2
- Gradle, Maven, Jitpack import HOT 1
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 orman.