9 votes

Fortnightly Programming Q&A Thread

General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.

Don't forget to format your code using the triple backticks or tildes:

Here is my schema:

```sql
CREATE TABLE article_to_warehouse (
  article_id   INTEGER
, warehouse_id INTEGER
)
;
```

How do I add a `UNIQUE` constraint?

24 comments

  1. [4]
    Comment deleted by author
    Link
    1. edenist
      Link Parent
      As someone who was in a similar position not too long ago, I can offer some of my own perspective and advice. Make sure you maintain industry best practices. This is both for the benefit of the...

      As someone who was in a similar position not too long ago, I can offer some of my own perspective and advice.

      Make sure you maintain industry best practices. This is both for the benefit of the organisation and also for yourself. As a sole operator it is really easy to take the shortest route to completion and develop on-the-fly.
      Use git [or some form of vcs], have a sane development, test and deployment pipeline. Given your resources, you can only do so much. Not saying you need to have a huge CI system with every bell and whistle, but have some procedure in place which states how things are done.

      Make sure your work passes the 'bus test'. If you were hit by a bus tonight, how would things be able to continue? As I said this isn't just for the organisation but also to make sure you keep yourself skilled and up to date. Things move fast in the software world, today's popular framework or paradigm is quickly replaced by something new. It's easy to get comfortable if a job is simple and you're on your own, but there's a reason these positions earn the title of being 'golden handcuffs'.

      I guess where I'm going with it all is that, make sure that if or when you need to move to another job, you are in a good position to do so. I moved from a small near-one-person-show to a large department, primarily as I still had good knowledge and experience.

      Honestly, if you don't have the managers and team members around you, just make the most of it! Focus on the technology and find the best ones to suit your needs.

      5 votes
    2. Akir
      Link Parent
      Just make sure that your superiors actually understand what you do for when future potential employers call them up. While a custom CMS sounds like it should be a positive on a resume, it actually...

      Just make sure that your superiors actually understand what you do for when future potential employers call them up.

      While a custom CMS sounds like it should be a positive on a resume, it actually doesn't help much. Lots of hiring is based on your familiarity with common frameworks, so if you can integrate with some of the bigger ones it would be better for your career.

      I'm basically in the same shoes as you right now, and a while back I tried my luck on the jobs market and was surprised at how little traction I got. That's a small part of why I decided to go back to school. The class I am taking right now has made me realize that the job that better describes what we do is actually that of an Analyst, so I'm making that my title. But I'm happier with my current job than I was in the past, so I won't know how effective it will be until much later.

      3 votes
    3. unknown user
      Link Parent
      Firstly, no managers and no colleagues? You lucky bastard! Seconldy, what kind of advice are you looking for exactly? It's not quite obvious from your post.

      Firstly, no managers and no colleagues? You lucky bastard!

      Seconldy, what kind of advice are you looking for exactly? It's not quite obvious from your post.

      1 vote
  2. [4]
    unknown user
    Link
    A question regarding HTML and CSS. I've noticed that while the height of <html> depends on the content, its width is always 100 % of the viewport, unless the content stretches that. Is that the...

    A question regarding HTML and CSS. I've noticed that while the height of <html> depends on the content, its width is always 100 % of the viewport, unless the content stretches that. Is that the standard width (that is, it is written in the standard) or is it an implementation-dependent detail? In other words, can I always count on the width being 100 % without putting width: 100% into my stylesheets explicitly?

    4 votes
    1. unknown user
      (edited )
      Link Parent
      My understanding is that width:100%; means "100% of the width of the closest relative-positioned block parent element, or root element". If you want to guarantee an element is the width of the...

      My understanding is that width:100%; means "100% of the width of the closest relative-positioned block parent element, or root element". If you want to guarantee an element is the width of the viewport, width:100vw; is what you want.

      4 votes
    2. [2]
      cfabbro
      Link Parent
      AFAIK width generally defaults to 'auto', which isn't really the same as '100%'.
      2 votes
      1. unknown user
        Link Parent
        Thank you for the links! I did some more digging and found this note on MDN: Which, combined with this quote from the CSS standard: Pretty much says that the width of <html> “has dimensions of the...

        Thank you for the links! I did some more digging and found this note on MDN:

        Note: The containing block in which the root element (<html>) resides is a rectangle called the initial containing block. It has the dimensions of the viewport (for continuous media) or the page area (for paged media).

        Which, combined with this quote from the CSS standard:

        10.3.3 Block-level, non-replaced elements in normal flow

        (…)

        If 'width' is set to 'auto', any other 'auto' values become '0' and 'width' follows from the resulting equality.

        Pretty much says that the width of <html> “has dimensions of the viewport”.

        3 votes
  3. [7]
    unknown user
    Link
    (I feel a bit uncomfortable being the only person who asks anything at all, but still…) An ed question: How do I replace all occurrences of a piece text between two patterns? This replaces one...

    (I feel a bit uncomfortable being the only person who asks anything at all, but still…)

    An ed question: How do I replace all occurrences of a piece text between two patterns? This replaces one occurrence:

    /BEGIN/,/END/c
    REDACTED
    .
    
    

    Is there a way to make this command “global”?

    4 votes
    1. [6]
      Gyrfalcon
      Link Parent
      I am not sure about ed, but in vim you would add a g flag to the end of the command. That may be worth a try. So your command would change to: /BEGIN/,/END/gc As a side question, why ed instead of...

      I am not sure about ed, but in vim you would add a g flag to the end of the command. That may be worth a try. So your command would change to:

      /BEGIN/,/END/gc
      

      As a side question, why ed instead of vim or any of its gui adaptations?

      3 votes
      1. [2]
        archevel
        Link Parent
        Isn't the command %s/searchpattern/replacement/g to search replace in vim for an entire file? Maybe I missremember...

        Isn't the command %s/searchpattern/replacement/g to search replace in vim for an entire file? Maybe I missremember...

        4 votes
        1. Gyrfalcon
          Link Parent
          Yes I think that is right. I suppose in this case in vim you would use visual mode to select only the areas that needed search and replace.

          Yes I think that is right. I suppose in this case in vim you would use visual mode to select only the areas that needed search and replace.

          1 vote
      2. [3]
        unknown user
        Link Parent
        Yeah, I know about that in vim, but in ed g is a command of the form g/RE/p (coincidentally, this is where the name “grep” comes from), so it takes a regexp instead of a couple of addresses, and...
        I am not sure about ed, but in vim you would add a g flag to the end of the command. That may be worth a try. So your command would change to:
        /BEGIN/,/END/gc

        Yeah, I know about that in vim, but in ed g is a command of the form g/RE/p (coincidentally, this is where the name “grep” comes from), so it takes a regexp instead of a couple of addresses, and “flags” aren't really a thing. I've read the entire POSIX standard description of ed and the GNU ed manual, but I couldn't find anything.

        ex actually can do g/BEGIN/,/END/c, so if everything else fails, I can always use that. I didn't want to use ex, because on most systems it's aliased to vim and so it is more heavy-weight and not really a command of its own.

        As a side question, why ed instead of vim or any of its gui adaptations?

        Several reasons in no particular order:

        • ed is the standard text editor.
        • IIRC, ken used ed until he and rob created sam, so it's an important part of Unix history.
        • sed -i is a non-POSIX extension, so there are basically only two ways to edit files from the command line in POSIX: ed and ex, unless there is a hidden editor I am not aware of. And I've already written why I prefer ed to ex.
        • Just for fun.
        • IIRC, it's the only line editor installed on most unices, so if you're interested in line editors, there isn't much to choose from.

        Any way, thanks for responding. I guess it goes into the “not quite possible” pile.

        4 votes
        1. [2]
          Gyrfalcon
          Link Parent
          The article and overall site you shared here are pretty neat! Reading a bit more about ed was nice. I am curious why you would like to use patterns to define your search space rather than line...

          Just for fun.

          The article and overall site you shared here are pretty neat! Reading a bit more about ed was nice. I am curious why you would like to use patterns to define your search space rather than line numbers, if that is something you wouldn't mind sharing.

          1 vote
          1. unknown user
            Link Parent
            I don't! I am quite active on the Russian StackOverflow. One user over there has recentrly asked a question about how one might replace all text between two strings in several texts from the...
            I am curious why you would like to use patterns to define your search space rather than line numbers, if that is something you wouldn't mind sharing.

            I don't! I am quite active on the Russian StackOverflow. One user over there has recentrly asked a question about how one might replace all text between two strings in several texts from the command line. I think they wanted to replace some copy-pasted HTML with a PHP include directive. One thing led to another and here I am :-)

            In general you don't know the exact line numbers when bulk-editing multiple files, so patterns feel like an obvious extension. I kind of wish there was a version of ed with sam's structured regular expressions.

            1 vote
  4. [2]
    unknown user
    Link
    I have a git question! Assuming I am on the branch foo, origin's foo (upstream) is 1234, and I need to force-push, is this: $ git push origin HEAD --force-with-lease=foo:1234 In any way more safe...

    I have a git question! Assuming I am on the branch foo, origin's foo (upstream) is 1234, and I need to force-push, is this:

    $ git push origin HEAD --force-with-lease=foo:1234
    

    In any way more safe than this:

    $ git push origin HEAD --force
    
    3 votes
    1. byron
      Link Parent
      The former having -with-lease helps some and it further "protects" more than a naked --force-with-lease (which I normally use) against you (or some program) running git fetch without you realizing...

      The former having -with-lease helps some and it further "protects" more than a naked --force-with-lease (which I normally use) against you (or some program) running git fetch without you realizing after someone else had pushed to origin/foo but before you can confirmed you're okay with the force push.

      4 votes
  5. [8]
    Gyrfalcon
    Link
    I was talking to @ThatFanficGuy about a header for my website, which I have posted here. I have finally gotten it to respond somewhat gracefully to the window changing width, but something I have...

    I was talking to @ThatFanficGuy about a header for my website, which I have posted here. I have finally gotten it to respond somewhat gracefully to the window changing width, but something I have done is making my content render under my header. My logo is an SVG so I haven't uploaded it anywhere yet, and all the HTML and CSS is from the final output of my static site generator, Jekyll.

    Does anyone have any idea why the content is doing this? Also, any general feedback? This is the first time I have tried web development.

    3 votes
    1. [5]
      unknown user
      Link Parent
      position: absolute Long story short: all content in HTML relies on something called the box model. It's why display: block elements – <h1>, <div>, <header> – take up all the width available and...

      but something I have done is making my content render under my header

      position: absolute

      Long story short: all content in HTML relies on something called the box model. It's why display: block elements – <h1>, <div>, <header> – take up all the width available and push the following content "down".

      When you position an element absolutely, not only do you make it stick to a set of coordinates within the page, you also take it out of the box model. This means that content will no longer pay attention to it when it comes to considering their own place on the page. In other words, what used to be a large block that takes up space, is no longer a block taking up space, as far as the rest of your content is concerned.

      If you do want your content to consider the large block, don't make the block use position: absolute.

      What was it that you meant to achieve using absolute positioning? There are different recommendations to what you want to achieve, and I think I know what you wanted to do. Since you're still learning, I'd like you to verbalize your goals on your own, because that's how you come to understand the problem you're trying to solve via web design. The more you talk through the stuff you're working with, the quicker you'll understand it.

      4 votes
      1. [4]
        Gyrfalcon
        Link Parent
        I think I did that because I have having trouble with the green area not aligning with the very top of the browser, which in turn was caused by an element within the green div being in the wrong...

        I think I did that because I have having trouble with the green area not aligning with the very top of the browser, which in turn was caused by an element within the green div being in the wrong position or maybe the wrong size or margins, pushing the whole thing down. I am not sure though, I spent a lot of time tinkering! Removing the absolute positioning does seem to have solved that quirk, so I will definitely keep in mind that the absolute positioning breaks things completely out of the box model.

        3 votes
        1. [3]
          unknown user
          Link Parent
          Keep in mind, for future reference, that there are plenty of reasons to use absolute positioning. Notifications are just one example. What might be an issue is that browsers set margin of body to...

          Keep in mind, for future reference, that there are plenty of reasons to use absolute positioning. Notifications are just one example.

          What might be an issue is that browsers set margin of body to a certain value (I believe it's 8px in Chrome), which pushes everything inwards, not letting it touch the boundries of the viewport (the part of the page that's in the view – as in, the visual container for all the elements that you can see while scrolling). Good intentions, but often leads to screwy interfaces.

          My go-to rule is to remove margin from body by setting it to 0. If I need the elements to not touch the viewport borders, I add padding: 2.5%. Doesn't do much on desktop and tablet screens, but helps maintain a reasonable boundry on smaller mobile screens.

          Oh, and also: there is an element called <header> in HTML already. You might want to use it instead of applying classes to <div>s everywhere, which isn't a good practice. HTML5 added a bunch of cool semantic elements that you might want to familiarize yourself with.

          3 votes
          1. [2]
            Gyrfalcon
            Link Parent
            I'll be sure to test out on Chrome and see if things are working properly, and I will definitely check out the <header> element. It was covered in some overviews of HTML I was looking at before I...

            I'll be sure to test out on Chrome and see if things are working properly, and I will definitely check out the <header> element. It was covered in some overviews of HTML I was looking at before I got started, but I forgot about it while I was tinkering!

            2 votes
            1. unknown user
              Link Parent
              No worries. I open DevDocs.io as a default action whenever I do web. Everyone searches for minor things all the time in this field.

              No worries. I open DevDocs.io as a default action whenever I do web. Everyone searches for minor things all the time in this field.

              1 vote
    2. [2]
      unknown user
      Link Parent
      One of the things I've learned over the years of fighting with CSS is that one is absolutely unable to do the classical header-content-footer trio right without CSS Flexbox or CSS Grid. You can...

      One of the things I've learned over the years of working fighting with CSS is that one is absolutely unable to do the classical header-content-footer trio right without CSS Flexbox or CSS Grid. You can see an example of how I do it with Flexbox on my personal website. The CSS is inline and mostly well-documented. The basic gist of it is:

      <!-- main.xhtml -->
      <!DOCTYPE html>
      <html>
      <!-- … -->
      <body>
      <header><!-- … --></header>
      <main><!-- … --></main>
      <footer><!-- … --></footer>
      </body>
      </html>
      
      /* main.css  */
      html { height: 100%; }
      body { display: flex; flex-direction: column; height: 100%; margin: 0; }
      main { flex-grow: 1; }
      

      Demo: https:// codepen.io/Ainar-G/pen/abbBGPj.

      Hopefully, this helps.

      3 votes
      1. Gyrfalcon
        Link Parent
        I think I've got it the way I want it by removing the absolute positioning on the top bar. I will keep in mind flexbox and grid. Grid looks especially useful because I have been thinking of making...

        I think I've got it the way I want it by removing the absolute positioning on the top bar. I will keep in mind flexbox and grid. Grid looks especially useful because I have been thinking of making some posts that have code and commentary sort of side by side, though I have no idea how to make that display well on mobile yet.

        3 votes