Re: How Alter Table to add foreign key to Django Models

2012-12-14 Thread Javier Guerra Giraldez
On Thu, Dec 13, 2012 at 7:53 AM, laxglx  wrote:
> Can anybody plz tell me how to add a foreign key an existing table using SQL
> Queries?
>>> I got the command, nut can't understand
>
> ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address)
> REFERENCES addresses (address) MATCH FULL;


as Chris has noted, this question as stated doesn't have anything to
do with Django.

what i'm guessing here is that you got that command from your database
docs and want to know how to apply that to modify your tables. right?

in principle, you shouldn't have to need that.  Django lets you define
the relationships in the model declaration, including foreign keys
constraints and indexes.

but if you have just added the foreign key, Django won't alter an
existing table.  (the syncdb command never modifies an existing table,
it only creates new tables when not found on the database)

if that's the case, you have three options:

- delete the table and redo the syncdb.  Django will create the table
with the current definition in the models.  pro: works every time.
con: you lose all existing data on that table.

- analyze the current table structure, what the new model represents
and manually issue the needed ALTER TABLEs.  pro: won't lose data if
done correctly.  con: not easy to do it right.

- use South.  it handles all the data structure migrations for you.
pro: very reliable, very easy, very maintanable. con: you have to
learn to use it (but it's not hard, and the docs are very good)


>
> What's  "distfk" here if it is key name what is (address) ? and what is
> addresses(address)?

"distfk" is the name of the constraint.  i don't think it's used
anywhere except an identifier.
the first "address" is the name of your referring field ("source
field") the second "address" (in fact "addresses(address)") is the
referred field ("target field").  typically it's more like
"address_id" for the first and "address(id)" for the second, since in
the vast majority of cases it's preferable that foreign key fields use
the id field for reference.

> And What does mean Match Full

that seems specific to your RDBMS.  i'll guess that it means use the
whole field to match.  typically on very long text fields wouldn't be
practical to copy the whole content to an index and to the referring
key, so it can use a length limited prefix.  Match Full sounds like
not to use any prefix, but the full field.


--
Javier

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: How Alter Table to add foreign key to Django Models

2012-12-14 Thread Chris Cogdon
It doesn't look like you're using Django at all. I suggest taking your 
question to the support community for the database you're using.



On Thursday, December 13, 2012 4:53:25 AM UTC-8, laxglx wrote:
>
> Can anybody plz tell me how to add a foreign key an existing table using 
> SQL Queries?
> >> I got the command, nut can't understand 
>
> ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) 
> REFERENCES addresses (address) MATCH FULL;
>
>
> What's  "distfk" here if it is key name what is (address) ? and what is 
> addresses(address)?
> And What does mean Match Full
>
>
> Thanks in advance 
> laxglx
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/BPaF9ehEQ7cJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: How Alter Table to add foreign key to Django Models

2012-12-14 Thread Chris Cogdon
Are you sure you're using Django? This SQL statement does not look like 
something django would have emitted. Perhaps your query is best taken to 
the support community for the database you're using. Our advice here may 
not be relevant to you.

The issue you're having is what the South package is designed to solve. 
South will want to see your model before you added the foreign key, but 
once you initialise it in that state, it will handle that for you.

To answer the specific questions (because I'm nice):

'distfk' is just a name for the constraint. It can be anything, really. The 
first 'address' is the column in the distributors table. The 
'addresses(address)' refers to the address column in the 'addresses' table. 
'MATCH FULL' is the type of uniqueness constraint you're putting in place. 
You should check the manual for the database for details. (looks like 
MySQL?)



On Thursday, December 13, 2012 4:53:25 AM UTC-8, laxglx wrote:
>
> Can anybody plz tell me how to add a foreign key an existing table using 
> SQL Queries?
> >> I got the command, nut can't understand 
>
> ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) 
> REFERENCES addresses (address) MATCH FULL;
>
>
> What's  "distfk" here if it is key name what is (address) ? and what is 
> addresses(address)?
> And What does mean Match Full
>
>
> Thanks in advance 
> laxglx
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/PDYq4XIh4BoJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



How Alter Table to add foreign key to Django Models

2012-12-13 Thread laxglx
Can anybody plz tell me how to add a foreign key an existing table using 
SQL Queries?
>> I got the command, nut can't understand 

ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES 
addresses (address) MATCH FULL;


What's  "distfk" here if it is key name what is (address) ? and what is 
addresses(address)?
And What does mean Match Full


Thanks in advance 
laxglx

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/kfd0FKyHs0sJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.