These forums have been archived and are now read-only.

The new forums are live and can be found at https://forums.eveonline.com/

EVE Technology Lab

 
  • Topic is locked indefinitely.
 

Foreign Keys in the SDE

First post First post
Author
Stacy Lone
Nirakura Inc
Decisions of Truth
#1 - 2014-10-19 13:01:40 UTC
So, i tried adding the foreign keys that are commented out in the SDE to the tables, e.g. this part:



Quote:
---
--- FOREIGN KEYS
---
ALTER TABLE agtAgents ADD CONSTRAINT agtAgents_FK_agent FOREIGN KEY (agentID) REFERENCES invNames(itemID)
ALTER TABLE agtAgents ADD CONSTRAINT agtAgents_FK_division FOREIGN KEY (divisionID) REFERENCES crpNPCDivisions(divisionID)
ALTER TABLE agtAgents ADD CONSTRAINT agtAgents_FK_corporation FOREIGN KEY (corporationID) REFERENCES crpNPCCorporations(corporationID)
ALTER TABLE agtAgents ADD CONSTRAINT agtAgents_FK_agentType FOREIGN KEY (agentTypeID) REFERENCES agtAgentTypes(agentTypeID)


This leaves me with:
Quote:
Msg 1778, Level 16, State 0, Line 6
Column 'invNames.itemID' is not the same data type as referencing column 'agtAgents.agentID' in foreign key 'agtAgents_FK_agent'.
Msg 1750, Level 16, State 0, Line 6
Could not create constraint. See previous errors.


Which is obvious, because invNames.itemID is bigint and agtAgents.agentID is int.

Since I imagine that CCP uses those FKs themselves, what is the magic to make them work? If I were to add ALTER statements to modify the clomuns I'd have to drop all FKs referencing the columns first, then alter the column, and then recreate all FKs, which would mean that i'd have to extract all those FKS from the dump.

Has anyone tried to get those FKs working, and if so, how?
CCP Nullarbor
C C P
C C P Alliance
#2 - 2014-10-19 21:46:35 UTC
Actually we don't use foreign keys on the TQ database for performance reasons.

CCP Nullarbor // Senior Engineer // Team Game of Drones

Dragonaire
Here there be Dragons
#3 - 2014-10-20 02:01:20 UTC
CCP Nullarbor wrote:
Actually we don't use foreign keys on the TQ database for performance reasons.

which since they don't use them explains why you found many that are outdated and don't work Blink The next question is why you think you need to use them? The reason I ask is that everything you are doing with the SDE should be read-only which won't get any type of performance increase as well by having them and will if anything be slowed down as well. What can be helpful is to look at the your own queries and maybe add a few extra indexes for stuff you do if one of your queries seem to be slower than you need/want it to be.

Finds camping stations from the inside much easier. Designer of Yapeal for the Eve API. Check out the Yapeal PHP API Library thread.

Stacy Lone
Nirakura Inc
Decisions of Truth
#4 - 2014-10-20 19:14:13 UTC
Maybe you should remove them from the SDE then? because i spent a very frustrating afternoon trying to understand what you guys might have done to get them working.


Using FKs allows ORM tools to create more meaningful mappings. I was trying to create JPA mappings for the SDE, and started by doing so manually. I got the whole inv* and dgm* tables mapped, but it started getting really tiresome at some point. So I took a closer look at whats in the SDE and found those FKs, and tried to get them to work, because automated reengeneering tools like those integrated into Hibernate can create those beans and mappings way better when there are foreign keys.

But if those keys are not longer used, I don't need to try to get them to work. Some of them still do, i'll take them and work from there and craft the rest of the mappings manually.

I could simply create one entity per table, but thats not really the point of proper ORM. I wanted richer beans that actually have those relationships mapped as attributes.

I don't really see why you'd think I ask about those FKs for performance reasons?


Anyways, thanks @CCP Nullarbor for clearing it up ;)
Dragonaire
Here there be Dragons
#5 - 2014-10-20 21:27:57 UTC
Since you are using an ORM you wouldn't notice the performance hit from the FKs because of the greater ORM overhead most of them have. As to probably why they left them in but commented out is so they can be used by people like you and me and maybe even the DB people at CCP much like a programmer comment their code to make things more clear when working with it later. It sounds like some of them have not been maintained as things have been updated like they should be but that really not uncommon from what I've seen else were in programming Blink

If I was to make a guess where most of them became dated was when CCP switch from 32 bit ints for IDs to 64 bit IDs so you could probably do a simple replace on the FK stuff to correct them. CCP has also changed/deleted some of the tables as well which is a harder thing to do updates for and would probably require you do them manually like you been doing.

Finds camping stations from the inside much easier. Designer of Yapeal for the Eve API. Check out the Yapeal PHP API Library thread.

Steve Ronuken
Fuzzwork Enterprises
Vote Steve Ronuken for CSM
#6 - 2014-10-21 01:26:30 UTC
ORMs are the devil.

They divorce people from the SQL mindset, and lead to some really /stupid/ things being thrown at the database. Sure, you can avoid them, but that's by understanding what's actually going on when you run an SQL query, and revising the object model, or writing your own query in the non-portable language of the ORM.

Woo! CSM XI!

Fuzzwork Enterprises

Twitter: @fuzzysteve on Twitter

Stacy Lone
Nirakura Inc
Decisions of Truth
#7 - 2014-10-21 14:40:20 UTC
Well, if you think ORMs are evil you must think that every layer of abstraction is evil and that we should still write ASM code?!

I have agreat understanding of databases and SQL, working with Oracle, IBM DB2, MySQl and other systems and also work with relational algebra, tuple-relations calculus and safe tuple relation calculus. So I really now my way around databases and I am actually able to write SQL queries like my second native language, for more then one RDBMS vendor.

Deeming a tool evil "just because" is short sighted at best, foolish at worst.

I agree with you to a certain degree that for someone ho doesn't know anything about databases, hiding their workings behind a framework can lead to obscure results as people do not understand the consequences of their actions and lack the understanding of interpreting the effects they see. But that can be said about any framework ever constructed, about any level of abtsraction ever built. Yet we do not longer write 0 und 1 to write our programs, we don't even write assembler code anymore. We have developed layer upon layer of abstraction and frameworks.

You are using Twitter Bottstrap on your very own site. Did you now that CSS frameworks are evil? They divorce people from the CSS mindset and lead to some really /stupid/ things being thrown at the browsers. Sure, you can avoid that, but that's by understanding what's actually going on in the browser when it parses and interprets the CSS file(s) and by writing your very own CSS declarations with all the hacks and tricks needed to get it working cross-browser or by solely targeting one browser and using only it's own proprietary features.

In short: Your usage of an abstraction layer contradicts your opposition of an abstraction layer in itself.

If you have ever developed enterprise applications then you'd know the values of ORM. But honestly I didn't come here for a fundamental discussion of the pros and cons of ORM systems. ORMs have - as all tools - their pros and cons, but what seperates a software developer / engineer who excels at his job from a mediocre one is the ability to weight the pros and cons of the given tools for the given task and to choose the proper one. Yet you jumped simply to the conclusion that ORMs are evil without any substantial argument for it, other then basically "just because", and without knowing what my goals really are. So yeah, maybe you want to give that a more in depth thought and decide in which situations ORMs are indeed not the best choice (hint: there are such situations - as well as there exist valid scenarios in which you'd use very low-level programming languages [hint: embedded systems]).

I've read many of your posts and I'm very well aware of the tools you offer and that you work much with the SDE. But honestly I am a little bit shocked that someone like you demonstrates such short sightedness. I had expected more. I guess the old saying "With a hammer, everything looks like a nail" is true after all.

Steve Ronuken
Fuzzwork Enterprises
Vote Steve Ronuken for CSM
#8 - 2014-10-21 19:22:25 UTC  |  Edited by: Steve Ronuken
Heh. I just have to deal with the fallout of developers who are using an ORM (Hibernate) and force them to rewrite bits of code, so it doesn't spam an insane number of queries at the database.

People who understand SQL can use ORMs to great effect. It's just it makes some people /think/ they can do database stuff, where as they just so so can't. Where they don't think about how they're accessing the data, so they don't produce appropriate indexes. Or where the data model itself is somewhat insane.

It's just my day job leaking through Smile

The number of times I've had to deal with complaints of 'The database is slow', when it's entirely down to someone having a join across five fairly large tables, without appropriate indexes... (yes, Mr 'the database is slow' you might want to slap an index on that column which is a foreign key, sure, lookups in one way are fast, but the other?) Big smile

Just a kneejerk reaction. In general, most eve projects aren't of a scale where an ORM will make a huge difference. And I'm always a fan of people learning SQL first, before then bringing in the abstraction layers to handle the boring crap of storing objects.


(Your example isn't a bad one, though I'd suggest the use of jquery is a better example, as it wraps pretty hard things up, in very simple to use ways.)

Woo! CSM XI!

Fuzzwork Enterprises

Twitter: @fuzzysteve on Twitter

Stacy Lone
Nirakura Inc
Decisions of Truth
#9 - 2014-10-21 19:42:37 UTC  |  Edited by: Stacy Lone
WelI can't agree more. But you really made it sound as if you were just against ORM without much considerations. My apologies if a came off a little bit blunt, in retrospect i my have worded it a little bit less controversial ;)

As I said i'm working on JPA bindings for the SDE, and I have already mapped all of the inv* and dgm* tables. It allowed me to prototype a basic fitting API in a matter of hours (no gui, only programmatically at this point). The beauty of having proper ORM mappings is that you don't need to concern yourself with actually reading the data (well, you have to pay attention to what data is fetched eagerly and what data is fetched lazily and a little bit about when to open & close sessions), but instead can focus on working with the data. Which makes it so much fun to work with.

Yes, jQuery might have been a better example, theres even a meme of it on stackoverflow (use jquery - http://meta.stackexchange.com/questions/45176/when-is-use-jquery-not-a-valid-answer-to-a-javascript-question) that is somewhat related, but Bootstrap was the first that came into my sight and had to do.

But for stupid mistakes, they also occur without ORM. I've seen bits of code where someone would slap 10k+ selects with quite complex onto the DB in a for-loop, just because he needed all of those entries. This kind of thing is painful. Though Hibernate can make some of the real bad mistakes harder to spot, especially for beginners. I definitely wouldn't advise anyone to use an ORM to learn how databases work. But on the other hand, i wouldn't recommend Zend Framework for someone who doesn't know a cent about OOP to learn PHP in an object oriented way either ;)

As I said I'm working with databases for years, both small sized and bigger ones, from different vendors, and even know some of the internal workings of them. So It's definitely not an issue about "learning SQL" for me (though I can always learn some new amazing facts about some RDMS that I didn't know before).